Last active
January 20, 2023 14:05
-
-
Save bennyistanto/e838e077f66058cb9a7dec5edd1367ee to your computer and use it in GitHub Desktop.
Generate Taylor Diagram from the output metrics of corrected data to the source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
NAME | |
taylor_diagram_outmetrics.py | |
Generate Taylor Diagram from all metrics output | |
DESCRIPTION | |
Input data for this script will use metric.csv generated by imerg_cpc_biascorrection.py. | |
REQUIREMENT | |
It required numpy, pandas, metpy, matplotlib and xarray module. So it will work on any machine environment | |
EXAMPLES | |
python taylor_diagram_outmetrics.py | |
NOTES | |
To visualize the metrics output using a Taylor diagram, you can use the taylor_diagram function | |
from the taylor_diagram module of the metpy library. This function allows you to plot the relative | |
bias, Pearson correlation coefficient, and root mean square error of a dataset on a Taylor diagram, | |
with reference to a reference dataset. | |
CONTACT | |
Benny Istanto | |
Climate Geographer | |
GOST, The World Bank | |
LICENSE | |
This script is in the public domain, free from copyrights or restrictions. | |
VERSION | |
$Id$ | |
TODO | |
xx | |
""" | |
import os | |
import xarray as xr | |
import pandas as pd | |
import numpy as np | |
from metpy.plots import taylor_diagram | |
import matplotlib.pyplot as plt | |
# Define the list of methods | |
methods = ['scale', 'distribution', 'delta', 'lsc', 'lscdf', 'rcdfm', 'mlr', | |
'ann', 'kalman', 'bcsd', 'bcsdm', 'qq_mapping', 'eQM', 'aQM', 'gQM', 'gpdQM'] | |
# Define the appropriate input and output directory paths | |
output_dir = f'output' | |
diagram_dir = f'{output_dir}/diagram' | |
# Create the output directories if they don't already exist | |
os.makedirs(output_dir, exist_ok=True) | |
os.makedirs(diagram_dir, exist_ok=True) | |
for method in methods: | |
# Load the metrics output from the bias correction | |
metrics = pd.read_csv(f'output/{method}/metrics/{method}_metrics.csv') | |
# Select the reference dataset (CPC in this example) and the corrected datasets (IMERG ann in this example) | |
ref = metrics[metrics['dataset'] == 'ref'] | |
corr = metrics[metrics['dataset'] == method] | |
# Extract the relative bias, Pearson correlation coefficient, and root mean square error for each dataset | |
ref_r, ref_p, ref_rmse = ref['relative_bias'], ref['pearson'], ref['rmse'] | |
corr_r, corr_p, corr_rmse = corr['relative_bias'], corr['pearson'], corr['rmse'] | |
# Create the Taylor diagram figure | |
fig = plt.figure(figsize=(10, 10)) | |
ax = fig.add_subplot(111, projection='polar') | |
# Add the reference dataset to the Taylor diagram | |
taylor_diagram(ref_rmse, ref_r, ref_p, marker='o', color='k', markersize=10, label='ref') | |
# Add the corrected dataset to the Taylor diagram | |
taylor_diagram(corr_rmse, corr_r, corr_p, marker='s', color='b', markersize=7, label=f'{method}') | |
# Add the gridlines and legend to the Taylor diagram | |
plt.grid(True) | |
ax.legend(loc='upper left') | |
# Save the Taylor diagram to a PNG file in the output folder | |
plt.savefig(f'{diagram_dir}/{method}_taylor_diagram.png', dpi=300, bbox_inches='tight') | |
# Show the Taylor diagram | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment