Skip to content
Snippets Groups Projects
Commit 42b9c572 authored by Frisinghelli Daniel's avatar Frisinghelli Daniel
Browse files

Update changes from dev branch.

parent 49f8a7f8
Branches master
No related tags found
No related merge requests found
......@@ -8,5 +8,7 @@ __pycache__/
# jupyter
.ipynb_checkpoints/
Notebooks/Figures/
*.nc
# netcdf files
Notebooks/*.nc
Notebooks/Figures/bootstrap/pr_members_vs_ensemble_extremes.png

138 KiB

Notebooks/Figures/bootstrap/pr_members_vs_ensemble_mean.png

141 KiB

Notebooks/Figures/bootstrap/pr_only_members_vs_ensemble_extremes.png

138 KiB

Notebooks/Figures/bootstrap/pr_only_members_vs_ensemble_mean.png

141 KiB

Notebooks/Figures/bootstrap/tasmax_members_vs_ensemble_extremes.png

132 KiB

Notebooks/Figures/bootstrap/tasmax_members_vs_ensemble_mean.png

127 KiB

%% Cell type:markdown id:b6344366-bbd8-4141-b229-2964a5875f76 tags:
# Plot bootstrapped model results
%% Cell type:code id:255d7312-58cd-41c3-a25b-f7c86bb8fe19 tags:
``` python
# builtins
import pathlib
# externals
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import gridspec
```
%% Cell type:code id:b357b52a-0093-42ff-b934-e6318f93ecca tags:
``` python
# path to bootstrapped model results
RESULTS = pathlib.Path('/mnt/CEPH_PROJECTS/FACT_CLIMAX/ERA5_PRED/bootstrap')
```
%% Cell type:code id:f2f9414a-2041-4b5a-8370-eb772ec5c31b tags:
``` python
# predictand
PREDICTAND = 'tasmin'
PREDICTAND = 'pr'
```
%% Cell type:code id:e5e7a63c-3d3c-4e29-94af-c21f0f63ba6c tags:
``` python
# whether only precipitation was used as predictor
PR_ONLY = False
PR_ONLY = True
```
%% Cell type:markdown id:354f45a0-0d3e-43db-bf0d-ede327b9e77c tags:
## Load model results
%% Cell type:code id:448c695b-e55e-4530-8f9b-661c58f8d81b tags:
``` python
# model results computed by notebook eval_bootstrap.ipynb
df_refe = pd.read_csv(RESULTS.joinpath(PREDICTAND, 'reference.csv'))
df_pred = pd.read_csv(RESULTS.joinpath(PREDICTAND, 'prediction_pr-only.csv' if PREDICTAND == 'pr' and PR_ONLY else
'prediction.csv'))
```
%% Cell type:code id:a6f2b9ea-3ede-40f8-a448-de2833b5d180 tags:
``` python
# dataframe of single members and ensembles only
members = df_pred[np.isin(df_pred['product'], ['Member-{}'.format(i) for i in range(10)])]
ensemble = df_pred[~np.isin(df_pred['product'], ['Member-{}'.format(i) for i in range(10)])]
```
%% Cell type:code id:1d9535d3-dbfc-4e65-b5df-728a98e2715f tags:
``` python
# create a sequential colormap: for reference data, single ensemble members, and ensemble mean predictions
palette = sns.color_palette('Blues', len(df_pred['loss'].unique()))
palette
```
%% Cell type:markdown id:f08e3d29-1692-441b-aee4-25c360dd6e8b tags:
### Mean values: single members vs. ensemble
%% Cell type:code id:c7bd9c53-4a96-41e7-b8bd-7d438d4dbe73 tags:
``` python
# initialize figure
fig = plt.figure(figsize=(16, 5))
# create grid for different subplots
grid = gridspec.GridSpec(ncols=5, nrows=1, width_ratios=[3, 1, 1, 3, 1], wspace=0.05, hspace=0)
# add subplots
ax1 = fig.add_subplot(grid[0])
ax2 = fig.add_subplot(grid[1], sharey=ax1)
ax3 = fig.add_subplot(grid[3])
ax4 = fig.add_subplot(grid[4], sharey=ax3)
axes = [ax1, ax2, ax3, ax4]
# plot bias: single members vs. ensemble
sns.barplot(x='product', y='bias', hue='loss', data=members, palette=palette, ax=ax1);
sns.barplot(x='product', y='bias', hue='loss', data=ensemble, palette=palette, ax=ax2);
# plot mae: single members vs. ensemble
sns.barplot(x='product', y='mae', hue='loss', data=members, palette=palette, ax=ax3);
sns.barplot(x='product', y='mae', hue='loss', data=ensemble, palette=palette, ax=ax4);
# axes limits and ticks
y_lim_bias = (-50, 50) if PREDICTAND == 'pr' else (-1, 1)
y_lim_mae = (0, 2) if PREDICTAND == 'pr' else (0, 1)
y_ticks_bias = (np.arange(y_lim_bias[0], y_lim_bias[1] + 10, 10) if PREDICTAND == 'pr' else
np.arange(y_lim_bias[0], y_lim_bias[1] + 0.2, 0.2))
y_ticks_mae = np.arange(y_lim_mae[0], y_lim_mae[1] + 0.2, 0.2)
# axis for bias
ax1.set_ylabel('Bias (%)' if PREDICTAND == 'pr' else 'Bias (°C)')
ax1.set_ylim(y_lim_bias)
ax1.set_yticks(y_ticks_bias)
# axis for mae
ax3.set_ylabel('Mean absolute error (mm)' if PREDICTAND == 'pr' else 'Mean absolute error (°C)')
ax3.set_ylim(y_lim_mae)
ax3.set_yticks(y_ticks_mae)
# adjust axis for ensemble predictions
for ax in [ax2, ax4]:
ax.yaxis.tick_right()
ax.set_ylabel('')
# axis fontsize and legend
for ax in axes:
ax.tick_params('both', labelsize=14)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
ax.yaxis.label.set_size(14)
ax.set_xlabel('')
# adjust legend
h, _ = ax.get_legend_handles_labels()
ax.get_legend().remove()
# show single legend
ax4.legend(bbox_to_anchor=(1.3, 1.05), loc=2, frameon=False, fontsize=14);
# save figure
fig.savefig('./Figures/bootstrap/{}_members_vs_ensemble_mean.png'.format(PREDICTAND), dpi=300, bbox_inches='tight')
filename = ('./Figures/bootstrap/pr_only_members_vs_ensemble_mean.png'.format(PREDICTAND) if
PREDICTAND == 'pr' and PR_ONLY else './Figures/bootstrap/{}_members_vs_ensemble_mean.png'.format(PREDICTAND))
fig.savefig(filename, dpi=300, bbox_inches='tight')
```
%% Cell type:markdown id:233f4239-1cca-4ad5-8581-782e42948503 tags:
### Extreme values: single members vs. ensemble
%% Cell type:code id:72aadc32-a1c8-40a1-8cbe-e7166e18b6e0 tags:
``` python
# initialize figure
fig = plt.figure(figsize=(16, 5))
# create grid for different subplots
grid = gridspec.GridSpec(ncols=5, nrows=1, width_ratios=[3, 1, 1, 3, 1], wspace=0.05, hspace=0)
# add subplots
ax1 = fig.add_subplot(grid[0])
ax2 = fig.add_subplot(grid[1], sharey=ax1)
ax3 = fig.add_subplot(grid[3])
ax4 = fig.add_subplot(grid[4], sharey=ax3)
axes = [ax1, ax2, ax3, ax4]
# plot bias: single members vs. ensemble
sns.barplot(x='product', y='bias_ex', hue='loss', data=members, palette=palette, ax=ax1);
sns.barplot(x='product', y='bias_ex', hue='loss', data=ensemble, palette=palette, ax=ax2);
# plot mae: single members vs. ensemble
sns.barplot(x='product', y='mae_ex', hue='loss', data=members, palette=palette, ax=ax3);
sns.barplot(x='product', y='mae_ex', hue='loss', data=ensemble, palette=palette, ax=ax4);
# axes limits and ticks
y_lim_bias = (-50, 50) if PREDICTAND == 'pr' else (-1, 1)
y_lim_mae = (0, 2) if PREDICTAND == 'pr' else (0, 2)
y_lim_mae = (0, 20) if PREDICTAND == 'pr' else (0, 2)
y_ticks_bias = (np.arange(y_lim_bias[0], y_lim_bias[1] + 10, 10) if PREDICTAND == 'pr' else
np.arange(y_lim_bias[0], y_lim_bias[1] + 0.2, 0.2))
y_ticks_mae = np.arange(y_lim_mae[0], y_lim_mae[1] + 0.2, 0.2)
y_ticks_mae = np.arange(y_lim_mae[0], y_lim_mae[1] + 2, 2) if PREDICTAND == 'pr' else np.arange(y_lim_mae[0], y_lim_mae[1] + 0.2, 0.2)
# axis for bias
ax1.set_ylabel('Bias (%)' if PREDICTAND == 'pr' else 'Bias (°C)')
ax1.set_ylim(y_lim_bias)
ax1.set_yticks(y_ticks_bias)
# axis for mae
ax3.set_ylabel('Mean absolute error (mm)' if PREDICTAND == 'pr' else 'Mean absolute error (°C)')
ax3.set_ylim(y_lim_mae)
ax3.set_yticks(y_ticks_mae)
# adjust axis for ensemble predictions
for ax in [ax2, ax4]:
ax.yaxis.tick_right()
ax.set_ylabel('')
# axis fontsize and legend
for ax in axes:
ax.tick_params('both', labelsize=14)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
ax.yaxis.label.set_size(14)
ax.set_xlabel('')
# adjust legend
h, _ = ax.get_legend_handles_labels()
ax.get_legend().remove()
# show single legend
ax4.legend(bbox_to_anchor=(1.3, 1.05), loc=2, frameon=False, fontsize=14);
# save figure
fig.savefig('./Figures/bootstrap/{}_members_vs_ensemble_extremes.png'.format(PREDICTAND), dpi=300, bbox_inches='tight')
filename = ('./Figures/bootstrap/pr_only_members_vs_ensemble_extremes.png'.format(PREDICTAND) if
PREDICTAND == 'pr' and PR_ONLY else './Figures/bootstrap/{}_members_vs_ensemble_extremes.png'.format(PREDICTAND))
fig.savefig(filename, dpi=300, bbox_inches='tight')
```
%% Cell type:markdown id:f13dce92-e895-48de-97ef-17b3d7b7553d tags:
### Predictions vs. reference
%% Cell type:code id:3b63c13d-b32a-4a24-8290-e209db490b88 tags:
``` python
df_refe
```
%% Cell type:code id:a5fea8e6-48f6-42b4-b72e-739e5eb9bf5c tags:
``` python
ensemble
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment