Skip to content

Commit

Permalink
Add plotting to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robbibt committed Dec 21, 2023
1 parent 60f46b0 commit a0a1632
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/test_intertidal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
import pickle
import rioxarray
import numpy as np
import matplotlib.pyplot as plt
from click.testing import CliRunner

from intertidal.elevation import intertidal_cli, elevation
Expand Down Expand Up @@ -71,6 +73,46 @@ def test_dem_accuracy(
assert accuracy_df.Bias < 0.2
assert abs(accuracy_df["Regression slope"] - 1) < 0.1

# Plot and compare - heatmap
plt.figure(figsize=(5, 5))
lim_min, lim_max = np.percentile(np.concatenate([validation_z, modelled_z]), [1, 99])
lim_min -= 0.2
lim_max += 0.2
sns.kdeplot(
x=validation_z,
y=modelled_z,
cmap="inferno",
fill=True,
ax=plt.gca(),
thresh=0,
bw_adjust=0.4,
levels=30,
)
plt.gca().set_facecolor("black")
plt.plot([lim_min, lim_max], [lim_min, lim_max], "--", c="white")
plt.margins(x=0, y=0)
plt.xlim(lim_min, lim_max)
plt.ylim(lim_min, lim_max)
plt.xlabel("Validation (m)")
plt.ylabel("Modelled (m)")
plt.title("Modelled vs validation elevation")

# Add stats annotation
plt.gca().annotate(
f'Correlation: {accuracy_df["Correlation"]:.2f}\n' \
f'R-squared: {accuracy_df["R-squared"]:.2f}\n' \
f'RMSE: {accuracy_df["RMSE"]:.2f} m\n' \
f'MAE: {accuracy_df["MAE"]:.2f} m\n' \
f'Bias: {accuracy_df["Bias"]:.2f} m\n' \
f'Slope: {accuracy_df["Regression slope"]:.2f}\n',
xy=(0.04, 0.7),
fontsize=9,
xycoords="axes fraction",
color="white",
)

plt.savefig(f"validation.jpg", dpi=150, bbox_inches="tight")


def test_elevation(satellite_ds):
ds, ds_aux, tide_m = elevation(
Expand Down

0 comments on commit a0a1632

Please sign in to comment.