if 'google.colab' in str(get_ipython()):
from google.colab import drive
'/content/drive', force_remount=False)
drive.mount(!pip install mirzai
else:
5.2. Observed vs. predicted scatterplots
Visualizing scatterplots of observed vs. predicted K ex. values for both PLR and CNN models.
# Python utils
from pathlib import Path
import pickle
from mirzai.vis.core import (centimeter, PRIMARY_COLOR,
set_style, DEFAULT_STYLE)
# Data vis.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import matplotlib.colors as mcolors
import matplotlib.cm as cm
# Data science stack
import numpy as np
import warnings
'ignore') warnings.filterwarnings(
Input data
To predict exchangeable potassium content with both the PLSR and CNN models, run the following notebooks: * PLSR training & evaluation * CNN training & evaluation
Instead, we load already predicted and saved values.
= Path('dumps')
src_dir = pickle.load(open(src_dir/'predicted-true-plsr-seed-1.pickle', "rb"))
y_hat_plsr, y_true_plsr = pickle.load(open(src_dir/'predicted-true-cnn-seed-1.pickle', "rb"))
y_hat_cnn, y_true_cnn print(f'y_hat_plsr shape: {y_hat_plsr.shape}, y_hat_cnn shape: {y_hat_cnn.shape}')
y_hat_plsr shape: (4014,), y_hat_cnn shape: (4014,)
Plot
def plot_hexbin_scatter(x, Y, ax=None, hb_kwargs={}):
if ax is None:
= plt.gca()
ax = ax.hexbin(x, Y, **hb_kwargs)
hb 'equal')
ax.set_aspect(return (ax, hb)
def get_color_norm(x, Y, ax, n_bins=5, hb_kwargs={}):
= ax.hexbin(x, Y, cmap='viridis', **hb_kwargs)
hb = np.rint(np.histogram_bin_edges(hb.get_array(), n_bins))
bounds 0] = bounds[0] + 1
bounds[= mcolors.BoundaryNorm(boundaries=bounds, ncolors=n_bins+1, extend='min')
norm return norm
def plot_obs_vs_pred(data_plsr, data_cnn,
=35,
gridsize=(16*centimeter,8*centimeter),
figsize=600):
dpi# Styles
= plt.rcParams
p "grid.color"] = "0.65"
p["grid.linewidth"] = 0.4
p[
# Layout
= plt.figure(figsize=figsize, dpi=dpi)
fig = 1, 3
nrows, ncols = 20, 20, 2
w1, w2, w3 = GridSpec(nrows=nrows, ncols=ncols, figure=fig, width_ratios=[w1, w2, w3])
gs = fig.add_subplot(gs[0, 0])
ax0 '(a)', loc='left')
ax0.set_title(= fig.add_subplot(gs[0, 1])
ax1 '(b)', loc='left')
ax1.set_title(
# color norm based on cnn perfs
= {'gridsize': gridsize, 'mincnt': 1, 'alpha': 0}
params_hb = data_cnn
x, Y = get_color_norm(x, Y, ax1, hb_kwargs=params_hb)
norm
# Calculate color scale adapted to grid resolution
for ax, (x, Y) in zip([ax0, ax1], [data_plsr, data_cnn]):
= {'gridsize': gridsize, 'cmap': cm.get_cmap('Spectral_r', norm.N),
params_hb 'mincnt': 1, 'norm': norm, 'linewidths': 0.2}
= plot_hexbin_scatter(x, Y, ax=ax, hb_kwargs=params_hb)
_, hb -1.25, 1.25], [-1.25, 1.25], 'k--', lw=0.75)
ax.plot([
= plt.subplot(gs[0, 2], aspect=w1)
ax_clb
ax_clb"xtick.direction"] = "out"
p[= plt.colorbar(hb, cax=ax_clb)
clb ='y', direction='out')
clb.ax.tick_params(axis'Counts', size=8)
clb.ax.set_title(
# Ornaments
'Observed ex-K ($log_{10}(cmol(+)kg^{-1})$) →', loc='top')
ax0.set_ylabel('Predicted ex-K ($log_{10}(cmol(+)kg^{-1})$) →', loc='right')
ax0.set_xlabel('Predicted ex-K ($log_{10}(cmol(+)kg^{-1})$) →', loc='right')
ax1.set_xlabel(
plt.tight_layout()
#FIG_PATH = Path('nameofyourfolder')
= Path('images/')
FIG_PATH
set_style(DEFAULT_STYLE)
plot_obs_vs_pred((y_hat_plsr, y_true_plsr), (y_hat_cnn, y_true_cnn))
# To save/export it
/'observed-vs-predicted.png', dpi=600, transparent=True, format='png') plt.savefig(FIG_PATH