From a9b81c2726269fdc8e91d417a4dd3e7f9ae0c868 Mon Sep 17 00:00:00 2001 From: Maxime Ballarotta Date: Mon, 21 Sep 2020 08:56:20 +0200 Subject: [PATCH] Results are now saved in the result folder + Add example_plot_results --- notebooks/baseline_oi.ipynb | 13 -- notebooks/example_data_eval.ipynb | 48 +++-- notebooks/example_plot_results.ipynb | 263 +++++++++++++++++++++++++++ src/mod_plot.py | 51 +++++- 4 files changed, 343 insertions(+), 32 deletions(-) create mode 100644 notebooks/example_plot_results.ipynb diff --git a/notebooks/baseline_oi.ipynb b/notebooks/baseline_oi.ipynb index 9809462..c2e60fe 100644 --- a/notebooks/baseline_oi.ipynb +++ b/notebooks/baseline_oi.ipynb @@ -44,8 +44,6 @@ "source": [ "import xarray as xr\n", "import numpy\n", - "import pyinterp\n", - "import dask\n", "import warnings\n", "import logging\n", "import sys\n", @@ -63,17 +61,6 @@ "logger.setLevel(logging.INFO)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cluster = dask.distributed.LocalCluster()\n", - "client = dask.distributed.Client(cluster)\n", - "client" - ] - }, { "cell_type": "code", "execution_count": null, diff --git a/notebooks/example_data_eval.ipynb b/notebooks/example_data_eval.ipynb index 521217d..b373773 100644 --- a/notebooks/example_data_eval.ipynb +++ b/notebooks/example_data_eval.ipynb @@ -56,9 +56,6 @@ "source": [ "import xarray as xr\n", "import numpy\n", - "import hvplot.xarray\n", - "import pyinterp\n", - "import dask\n", "import warnings\n", "import xrft\n", "import logging\n", @@ -78,17 +75,6 @@ "logger.setLevel(logging.INFO)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cluster = dask.distributed.LocalCluster()\n", - "client = dask.distributed.Client(cluster)\n", - "client" - ] - }, { "cell_type": "code", "execution_count": null, @@ -198,6 +184,36 @@ "psd_oi1, leaderboard_psds_score, leaderboard_psdt_score = psd_based_scores(ds_oi1_regrid, dc_ref_sample)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define outputs\n", + "output_directory = '../results/'\n", + "if not os.path.exists(output_directory):\n", + " os.mkdir(output_directory)\n", + "filename_rmse_t = output_directory + 'rmse_t_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "filename_rmse_xy = output_directory + 'rmse_xy_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "filename_psd = output_directory + 'psd_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "filename_dc_ref_sample = output_directory + 'dc_ref_2012-10-22-2012-12-02_sample.nc'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Save results\n", + "rmse_t_oi1.to_netcdf(filename_rmse_t)\n", + "rmse_xy_oi1.to_netcdf(filename_rmse_xy)\n", + "psd_oi1.name = 'psd_score'\n", + "psd_oi1.to_netcdf(filename_psd)\n", + "dc_ref_sample.to_netcdf(filename_dc_ref_sample)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -252,9 +268,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# TODO : Add the plot (serie temporelle nrmse, psd score, spatial pattern rmse ????)" - ] + "source": [] } ], "metadata": { diff --git a/notebooks/example_plot_results.ipynb b/notebooks/example_plot_results.ipynb new file mode 100644 index 0000000..03c6ea4 --- /dev/null +++ b/notebooks/example_plot_results.ipynb @@ -0,0 +1,263 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# IV- Demo. plot results" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "An example of data evaluation metrics have been produced in the \"demo_data_eval.ipynb\" notebook. Here, we illustrate the results. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import xarray as xr\n", + "import hvplot.xarray\n", + "import numpy\n", + "import sys" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sys.path.append('..')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from src.mod_plot import *" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Load the reference SSH field" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename_ssh_reference = '../results/dc_ref_2012-10-22-2012-12-02_sample.nc'\n", + "dc_ref_sample = xr.open_dataset(filename_ssh_reference)\n", + "dc_ref_sample" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Load the reconstructed SSH field" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename_ssh_reconstruction_regridded = '../results/ssh_reconstruction_regridded_2012-10-22-2012-12-02_jason1.nc'\n", + "dc_reconstruction_regridded = xr.open_dataset(filename_ssh_reconstruction_regridded)\n", + "dc_reconstruction_regridded" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename_ssh_reconstruction = '../results/ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "dc_reconstruction = xr.open_dataset(filename_ssh_reconstruction)\n", + "dc_reconstruction" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Display the reference & reconstructed SSH fields" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds_concat = xr.concat((dc_ref_sample, dc_reconstruction_regridded), dim='field')\n", + "ds_concat['field'] = [\"Nature Run\", \"OI Reconstruction\"]\n", + "ds_concat" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds_concat.hvplot.image(x='lon', y='lat', cmap='gist_stern', by='field', width=500, height=400, widget_location='bottom', clabel='SSH [m]', subplots=True, clim=(-0.5, 1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Display the difference SSH_reconstruction minus SSH_reference" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "(dc_reconstruction_regridded - dc_ref_sample).hvplot.image(x='lon', y='lat', cmap='coolwarm', width=500, height=400, widget_location='bottom', clabel='dSSH[m]')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Display spatially averaged daily RMSE score (normalized RMSE)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Shows the temporal stability of the reconstuction. A score of 1 means a perfect reconstruction, a zero score means strong incoherency between the reconstruction & reference fields " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename_rmse_t = '../results/rmse_t_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "ds = xr.open_dataset(filename_rmse_t)\n", + "ds.rmse_t.hvplot(yaxis='left', title='Spatially averaged daily RMSE', ylim=(0, 1), ylabel='RMSE score')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dc_reconstruction.nobs.hvplot.area(color='grey', title='# obs in OI')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Display temporally averaged RMSE" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Highlight the regions of large/low mean reconstruction errors" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename_rmse_xy = '../results/rmse_xy_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "ds = xr.open_dataset(filename_rmse_xy)\n", + "ds.rmse_xy.hvplot(x='lon', y='lat', cmap='Reds', width=500, height=400, clabel='RMSE[m]', title='Temporally averaged RMSE')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Display PSD score" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Highlight the average space-time scales that are resolved in the reconstruction. The boundary is delimited by the iso-contour 0.5 (green area in the figure)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename_psd = '../results/psd_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n", + "ds = xr.open_dataset(filename_psd)\n", + "plot_psd_score(ds)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ds['freq_time'] = 1./ds['freq_time']\n", + "ds['freq_lon'] = 1./ds['freq_lon']\n", + "ds.hvplot.quadmesh(x='freq_lon', y='freq_time', z='psd_score', cmap='RdYlGn',\n", + " clim=(0, 1), logx=False, logy=False, title='PSD-score', width=500, height=400, \n", + " clabel='PSD score', xlabel='wavelenght (degree)', ylabel='wavelenght (days)')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/src/mod_plot.py b/src/mod_plot.py index b8a1309..de8c503 100644 --- a/src/mod_plot.py +++ b/src/mod_plot.py @@ -4,7 +4,8 @@ import matplotlib.pylab as plt from matplotlib.ticker import ScalarFormatter -def plot_psd_score(ds_psd): + +def plot_psd_score_v0(ds_psd): try: nb_experiment = len(ds_psd.experiment) @@ -138,4 +139,50 @@ def hvplot_demo_obs_karin(list_of_dataset, dc_ref, central_date, delta_t): dc_ref_reformated.hvplot.quadmesh(x='lon', y='lat', color='ssh_model', height=300, width=400, cmap = 'gist_stern', datashade=True) - return plot \ No newline at end of file + return plot + + +def plot_psd_score(ds): + + plt.figure(figsize=(8, 6)) + ax = plt.gca() + #ax.invert_yaxis() + #ax.invert_xaxis() + c1 = plt.contourf(1./(ds['freq_lon']), 1./ds['freq_time'], ds['psd_score'], + levels=numpy.arange(0,1.1, 0.1), cmap='RdYlGn', extend='both') + cbar = plt.colorbar(pad=0.01) + plt.xlabel('wavelenght(degree_lon)', fontweight='bold', fontsize=20) + plt.ylabel('wavelenght (days)', fontweight='bold', fontsize=20) + #plt.xscale('log') + #plt.yscale('log') + plt.grid(linestyle='--', lw=1, color='w') + plt.xticks(fontsize=18) + plt.yticks(fontsize=18) + plt.title('PSD-based score', fontweight='bold', fontsize=20) + for axis in [ax.xaxis, ax.yaxis]: + axis.set_major_formatter(ScalarFormatter()) + c2 = plt.contour(1./(ds['freq_lon']), 1./ds['freq_time'], ds['psd_score'], levels=[0.5], linewidths=2, colors='k') + cbar.add_lines(c2) + + bbox_props = dict(boxstyle="round,pad=0.5", fc="w", ec="k", lw=2) + ax.annotate('Resolved scales', + xy=(1.15, 0.8), + xycoords='axes fraction', + xytext=(1.15, 0.55), + bbox=bbox_props, + arrowprops= + dict(facecolor='black', shrink=0.05), + horizontalalignment='left', + verticalalignment='center') + + ax.annotate('UN-resolved scales', + xy=(1.15, 0.2), + xycoords='axes fraction', + xytext=(1.15, 0.45), + bbox=bbox_props, + arrowprops= + dict(facecolor='black', shrink=0.05), + horizontalalignment='left', + verticalalignment='center') + + plt.show() \ No newline at end of file