Compare VS-Lite in pure Python with the wrappers

In this notebook, we compare VS-Lite in pure Python with the wrappers in R and Matlab.

[1]:
%load_ext autoreload
%autoreload 2

import PyVSL
import pandas as pd
import numpy as np

Load test data

First of all, we load the test data (can be downloaded with this link), which contains T and P at a grid point of the CCSM4 past1000 simulation. Note that the unit for T is degree C and that for P is accumulated monthly precipitation (mm/month).

[2]:
data_dict = pd.read_pickle('./data/test_T_P.pkl')
time = data_dict['time']
T = data_dict['T'] # unit: degC
P = data_dict['P'] # unit: mm/month

Set parameters

[3]:
T1, T2 = 1, 15
M1, M2 = 0.01, 0.05
syear, eyear, phi = 850, 1850, 45

Run VS-Lite in pure Python

[4]:
%%time

res_P = PyVSL.VSL(
    syear, eyear, phi,               # the starting year and ending year of the input T & P, along with the latitude
    T, P,
    T1=T1, T2=T2, M1=M1, M2=M2,  # parameters of the thresholds
)
CPU times: user 279 ms, sys: 1.45 ms, total: 280 ms
Wall time: 280 ms

Run VS-Lite in Matlab

[5]:
%%time



res_M = PyVSL.VSL_M(
    syear, eyear, phi,               # the starting year and ending year of the input T & P, along with the latitude
    T, P,
    T1=T1, T2=T2, M1=M1, M2=M2,  # parameters of the thresholds
)
CPU times: user 7.16 ms, sys: 10.9 ms, total: 18.1 ms
Wall time: 1.57 s

Run VS-Lite in R

[6]:
%%time

res_R = PyVSL.VSL_R(
    syear, eyear, phi,               # the starting year and ending year of the input T & P, along with the latitude
    T, P,
    T1=T1, T2=T2, M1=M1, M2=M2,  # parameters of the thresholds
)
CPU times: user 167 ms, sys: 24.2 ms, total: 191 ms
Wall time: 191 ms

Compare the results

[7]:
from sklearn.metrics import mean_squared_error as MSE

def compare_res(res_A, res_B):
    print(MSE(res_A['trw'], res_B['trw']))
[8]:
compare_res(res_P, res_M)
compare_res(res_P, res_R)
compare_res(res_M, res_R)
2.4962545261838044e-07
2.496254599952891e-07
7.37388906089954e-15

Great! The MSE between the simulated TRW from the three functions are close to each other.

[ ]: