r_vs_python_arguments.Rmd
Building on the default functionality shared between the two languages, this vignette goes further to look at how changing the default arguments may affect the results. Along the way the outputs are checked for consistency and all of the arguments for all of the functions are tweaked to look for irregularities. This is necessary before only one language is used to further investigate best practices for dealing with sub-optimal time series in the detection of MHWs.
Preferably this step could have been done programmatically, without creating an army of outputs as seen below. The R package reticulate
does not seem to want to play along though so I must rather run the Python code, save the outputs, load them into R and compare the results natively in just R… So let’s get started.
import numpy as np
from datetime import date
import pandas as pd
import marineHeatWaves as mhw
# The date values
t = np.arange(date(1982,1,1).toordinal(),date(2014,12,31).toordinal()+1)
# The temperature values
sst = np.loadtxt(open("data/sst_WA.csv", "r"), delimiter = ',', skiprows = 1)
# The default python output from the previous comparison vignette
default_py_event <- read_csv("data/mhws_py.csv")
default_py_clim <- read_csv("data/clim_py.csv")
# The default R output
default_r <- detect_event(ts2clm(data = sst_WA,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
compare_event <- function(res_event_R, res_event_Python){
# Remove non-numeric columns
res_event_num <- res_event_R %>%
select_if(is.numeric)
# Run the loop
res_event_cor <- data.frame()
for(i in 1:length(colnames(res_event_num))){
if(colnames(res_event_num)[i] %in% colnames(res_event_Python)){
x1 <- res_event_R[colnames(res_event_R) == colnames(res_event_num)[i]]
x2 <- res_event_Python[colnames(res_event_Python) == colnames(res_event_num)[i]]
x <- data.frame(r = cor(x1, x2), var = colnames(res_event_num)[i])
colnames(x)[1] <- "r"
rownames(x) <- NULL
} else {
x <- data.frame(r = NA, var = colnames(res_event_num)[i])
}
res_event_cor <- rbind(res_event_cor, x)
}
return(res_event_cor)
}
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999996
## [1] -6.573385
## [1] 0.1630294
## [1] 0.008673771
# Compare default events
default_event <- compare_event(default_r$event, default_py_event)
default_event
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999991 intensity_mean
## 7 0.9999997 intensity_max
## 8 0.9996073 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999971 intensity_mean_relThresh
## 11 0.9999994 intensity_max_relThresh
## 12 0.9995693 intensity_var_relThresh
## 13 0.9999996 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9990733 intensity_max_abs
## 16 0.9997263 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999999 rate_onset
## 19 0.9999999 rate_decline
pctile
comparisonsHere we look at a few possible settings for the pctile
argument and ensure that everything matches up.
First run and save the Python output.
# pctile = 80
mhws, clim = mhw.detect(t, sst, pctile = 80)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_pctile_80.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_pctile_80.csv', sep = ',', index = False)
# pctile = 95
mhws, clim = mhw.detect(t, sst, pctile = 95)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_pctile_95.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_pctile_95.csv', sep = ',', index = False)
# pctile = 99
mhws, clim = mhw.detect(t, sst, pctile = 99)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_pctile_99.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_pctile_99.csv', sep = ',', index = False)
Then run the R output.
pctile_80_r <- detect_event(ts2clm(data = sst_WA, pctile = 80,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
pctile_95_r <- detect_event(ts2clm(data = sst_WA, pctile = 95,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
pctile_99_r <- detect_event(ts2clm(data = sst_WA, pctile = 99,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
Lastly load the Python output and compare the two.
# Load Python
pctile_80_py_event <- read_csv("data/mhws_py_pctile_80.csv")
pctile_80_py_clim <- read_csv("data/clim_py_pctile_80.csv")
pctile_95_py_event <- read_csv("data/mhws_py_pctile_95.csv")
pctile_95_py_clim <- read_csv("data/clim_py_pctile_95.csv")
pctile_99_py_event <- read_csv("data/mhws_py_pctile_99.csv")
pctile_99_py_clim <- read_csv("data/clim_py_pctile_99.csv")
# Compare clims and intensities
# 80
cor(pctile_80_r$climatology$seas, pctile_80_py_clim$seas)
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999982
## [1] -14.013
## [1] 2.444108
## [1] -1.102694
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999999
## [1] -3.402085
## [1] 0.06129223
## [1] 0.007555896
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999944
## [1] 28.39465
## [1] -0.003142188
## [1] -0.0002594781
# Compare events
# pctile_80_event <- compare_event(pctile_80_r$event, pctile_80_py_event)
pctile_95_event <- compare_event(pctile_95_r$event, pctile_95_py_event)
pctile_95_event
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999988 intensity_mean
## 7 0.9999996 intensity_max
## 8 0.9988679 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999979 intensity_mean_relThresh
## 11 0.9999468 intensity_max_relThresh
## 12 0.9988432 intensity_var_relThresh
## 13 0.9999999 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9997819 intensity_max_abs
## 16 0.9990657 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999998 rate_onset
## 19 1.0000000 rate_decline
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 1.0000000 intensity_mean
## 7 1.0000000 intensity_max
## 8 0.9995120 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999043 intensity_mean_relThresh
## 11 0.9999951 intensity_max_relThresh
## 12 0.9993619 intensity_var_relThresh
## 13 0.9999383 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 1.0000000 intensity_max_abs
## 16 0.9991278 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 1.0000000 rate_onset
## 19 1.0000000 rate_decline
There appears to be an issue with the results when the threshold is set to pctile = 80
as Python detects one more event than R. Let’s take a closer look.
pctile_80_r_event <- pctile_80_r$event
pctile_80_py_event$date_start[!pctile_80_py_event$date_start %in% pctile_80_r_event$date_start]
## [1] "2013-12-30"
The issue appears to be that the R code is joining across a four day gap, which it shouldn’t do. So I assume that the threshold is different between the languages. This is curious as the seasonal and threshold climatologies correlate nearly perfectly. Diving deeper, let’s pull out the clim values around the time of this event and see what we see.
pctile_80_r_clim <- pctile_80_r$climatology
pctile_80_r_clim_sub <- pctile_80_r_clim[11670:11720, ]
pctile_80_py_clim_sub <- pctile_80_py_clim[11670:11720, ]
r_fig <- ggplot(pctile_80_r_clim_sub, aes(x = t, y = temp)) +
geom_flame(aes(y2 = thresh)) +
geom_line() +
geom_line(aes(y = thresh), colour = "Red") +
geom_line(aes(y = seas), colour = "grey20") +
ggtitle("R code")
py_fig <- ggplot(pctile_80_py_clim_sub,
aes(x = pctile_80_r_clim_sub$t, y = pctile_80_r_clim_sub$temp)) +
geom_flame(aes(y2 = thresh)) +
geom_line() +
geom_line(aes(y = thresh), colour = "Red") +
geom_line(aes(y = seas), colour = "grey20") +
ggtitle("Python code")
ggarrange(r_fig, py_fig, ncol = 1, nrow = 2)
As we may see in the figure above, the difference is not perceptible to the human eye. Looking very closely at the clim outputs manually one may see that in the R code, the threshold on 2013-12-29
is just below the temperature value due to a rounding difference from the Python code at the third decimal place.
Also above one may see that even though the values correlate nearly perfectly, the sum of the seasonal climatology value output by R are 1.98122
less than the Python output. Considering that there are 12053 values, the average difference is only 0.00016
, which is not terribly much at all. The issue appears to be in the calculation of the threshold climatology. This value is not consistently different based on the pctile
value used. It varies quite a bit. With the sum difference at pctile = 80
being -13.86432
, the default of pctile = 90
being -6.563516
, pctile = 95
being -3.382435
, and pctile = 99
being 28.37769
. This is strong evidence that the threshold values are being calculated using some different method that needs to be addressed. That being said, even at the largest difference of 28.37769
, that is still only an average difference per row of 0.002354409
Digging deeper still, let’s pull up the old RmarineHeatWaves
code and run another comparison with that to see if this difference is due to newer changes in the development of the heatwaveR
package.
ts <- RmarineHeatWaves::make_whole(sst_WA)
default_r_old <- RmarineHeatWaves::detect(data = ts, climatology_start = "1982-01-01", climatology_end = "2014-12-31")
default_r_old_event <- default_r_old$event
default_r_old_clim <- default_r_old$clim
# Compare default new and old R clims and intensities
cor(default_r$climatology$seas, default_r_old_clim$seas_clim_year)
## [1] 1
## [1] -0.07260093
## [1] 1
## [1] -0.009868637
## [1] 0.1630294
## [1] 0.008673771
# Compare default new and old R clims
default_event <- compare_event(default_r$event, default_r_old_event)
default_event
## r var
## 1 1 event_no
## 2 1 index_start
## 3 NA index_peak
## 4 NA index_end
## 5 1 duration
## 6 NA intensity_mean
## 7 NA intensity_max
## 8 NA intensity_var
## 9 NA intensity_cumulative
## 10 NA intensity_mean_relThresh
## 11 NA intensity_max_relThresh
## 12 NA intensity_var_relThresh
## 13 NA intensity_cumulative_relThresh
## 14 NA intensity_mean_abs
## 15 NA intensity_max_abs
## 16 NA intensity_var_abs
## 17 NA intensity_cumulative_abs
## 18 1 rate_onset
## 19 1 rate_decline
# Compare Python and old R clims and intensities
cor(default_py_clim$seas, default_r_old_clim$seas_clim_year)
## [1] 0.9999999
## [1] 1.98122
## [1] 0.9999996
## [1] 6.563516
## [1] -0.157388
## [1] -0.008520925
# Compare Python and old R clims
default_event <- compare_event(default_r_old_event, default_py_event)
default_event
## r var
## 1 1 index_start
## 2 NA index_stop
## 3 NA event_no
## 4 1 duration
## 5 NA int_mean
## 6 NA int_max
## 7 NA int_var
## 8 NA int_cum
## 9 NA int_mean_rel_thresh
## 10 NA int_max_rel_thresh
## 11 NA int_var_rel_thresh
## 12 NA int_cum_rel_thresh
## 13 NA int_mean_abs
## 14 NA int_max_abs
## 15 NA int_var_abs
## 16 NA int_cum_abs
## 17 NA int_mean_norm
## 18 NA int_max_norm
## 19 1 rate_onset
## 20 1 rate_decline
The differences between the old and new R implementation are minute to the point of being almost imperceptible.
smoothPercentileWidth
comparisonsSeeing now that the choice of the pctile
threshold may create results different enough as to produce a different counts of events, we need to look closer at the different arguments. I have my suspicions that the smoothing of the threshold values may be the culprit here.
spw_no_r <- detect_event(ts2clm(data = sst_WA, smoothPercentile = FALSE,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
spw_11_r <- detect_event(ts2clm(data = sst_WA, smoothPercentileWidth = 11,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
spw_51_r <- detect_event(ts2clm(data = sst_WA, smoothPercentileWidth = 51,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
# smoothPercentileWidth = FALSE
mhws, clim = mhw.detect(t, sst, smoothPercentile = False)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_spw_no.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_spw_no.csv', sep = ',', index = False)
# smoothPercentileWidth = 11
mhws, clim = mhw.detect(t, sst, smoothPercentileWidth = 11)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_spw_11.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_spw_11.csv', sep = ',', index = False)
# smoothPercentileWidth = 51
mhws, clim = mhw.detect(t, sst, smoothPercentileWidth = 51)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_spw_51.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_spw_51.csv', sep = ',', index = False)
# Load Python
spw_no_py_event <- read_csv("data/mhws_py_spw_no.csv")
spw_no_py_clim <- read_csv("data/clim_py_spw_no.csv")
spw_11_py_event <- read_csv("data/mhws_py_spw_11.csv")
spw_11_py_clim <- read_csv("data/clim_py_spw_11.csv")
spw_51_py_event <- read_csv("data/mhws_py_spw_51.csv")
spw_51_py_clim <- read_csv("data/clim_py_spw_51.csv")
# Compare clims and intensities
# no smoothing
cor(spw_no_r$climatology$seas, spw_no_py_clim$seas)
## [1] 0.9999992
## [1] -2.124743
## [1] 0.9999965
## [1] -6.492852
## [1] 0.1128817
## [1] 0.01567827
## [1] 0.9999997
## [1] -2.053809
## [1] 0.999999
## [1] -6.294534
## [1] 0.1236748
## [1] 0.006339516
## [1] 0.9999999
## [1] -2.049584
## [1] 0.9999998
## [1] -6.601041
## [1] 0.1554402
## [1] 0.006590975
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999980 intensity_mean
## 7 0.9999991 intensity_max
## 8 0.9993833 intensity_var
## 9 0.9999999 intensity_cumulative
## 10 0.9999956 intensity_mean_relThresh
## 11 0.9997743 intensity_max_relThresh
## 12 0.9993307 intensity_var_relThresh
## 13 0.9999995 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9989767 intensity_max_abs
## 16 0.9995815 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999991 rate_onset
## 19 0.9999993 rate_decline
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999983 intensity_mean
## 7 0.9999994 intensity_max
## 8 0.9994320 intensity_var
## 9 0.9999999 intensity_cumulative
## 10 0.9999963 intensity_mean_relThresh
## 11 0.9999581 intensity_max_relThresh
## 12 0.9994217 intensity_var_relThresh
## 13 0.9999996 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9989804 intensity_max_abs
## 16 0.9996098 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999985 rate_onset
## 19 0.9999995 rate_decline
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999995 intensity_mean
## 7 0.9999999 intensity_max
## 8 0.9996564 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999968 intensity_mean_relThresh
## 11 0.9999679 intensity_max_relThresh
## 12 0.9996309 intensity_var_relThresh
## 13 0.9999996 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9992145 intensity_max_abs
## 16 0.9997404 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 1.0000000 rate_onset
## 19 1.0000000 rate_decline
Surprisingly the smoothing window, regardless if it is applied or not, seems to make little difference. But again, let’s compare the old R code against Python as well.
ts <- RmarineHeatWaves::make_whole(sst_WA)
spw_no_r_old <- RmarineHeatWaves::detect(data = ts, climatology_start = "1982-01-01", climatology_end = "2014-12-31", smooth_percentile = FALSE)
spw_no_r_old_event <- spw_no_r_old$event
spw_no_r_old_clim <- spw_no_r_old$clim
# Compare new and old R code
sum(spw_no_r$climatology$seas) - sum(spw_no_r_old_clim$seas_clim_year)
## [1] -0.06278893
## [1] 0.006147504
# Compare old & new R against Python
sum(spw_no_py_clim$seas) - sum(spw_no_r_old_clim$seas_clim_year)
## [1] 2.061954
## [1] 2.124743
## [1] 6.499
## [1] 6.492852
Again, almost no difference.
windowHalfWidth
comparisonsWe’ve now seen that the differences between both the new & old R code and the Python code hold consistently different regardless of the changing of the pctile
or smoothWindowPercentile
. This then leads me to think that this is just a rounding issue between the two languages, and that there is not some fundamentally different calculation happening somewhere in the code. That being said, it is still necessary to be thorough and so I’ll go through the rest of the arguments now. Starting with windowHalfWidth
.
whw_3_r <- detect_event(ts2clm(data = sst_WA, windowHalfWidth = 3,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
whw_7_r <- detect_event(ts2clm(data = sst_WA, windowHalfWidth = 7,
climatologyPeriod = c("1982-01-01", "2014-12-31")))
# windowHalfWidth = 3
mhws, clim = mhw.detect(t, sst, windowHalfWidth = 3)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_whw_3.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_whw_3.csv', sep = ',', index = False)
# windowHalfWidth = 7
mhws, clim = mhw.detect(t, sst, windowHalfWidth = 7)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_whw_7.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_whw_7.csv', sep = ',', index = False)
# Load Python
whw_3_py_event <- read_csv("data/mhws_py_whw_3.csv")
whw_3_py_clim <- read_csv("data/clim_py_whw_3.csv")
whw_7_py_event <- read_csv("data/mhws_py_whw_7.csv")
whw_7_py_clim <- read_csv("data/clim_py_whw_7.csv")
# Compare clims and intensities
# 3 days
cor(whw_3_r$climatology$seas, whw_3_py_clim$seas)
## [1] 0.9999999
## [1] -1.284023
## [1] 0.9999999
## [1] -3.022406
## [1] 0.103755
## [1] 0.005514523
## [1] 0.9999998
## [1] -2.859033
## [1] 0.999998
## [1] -13.58322
## [1] 8.303694
## [1] 1.901248
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999997 intensity_mean
## 7 0.9999999 intensity_max
## 8 0.9996480 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999993 intensity_mean_relThresh
## 11 0.9999938 intensity_max_relThresh
## 12 0.9996042 intensity_var_relThresh
## 13 0.9999999 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9993263 intensity_max_abs
## 16 0.9997486 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 1.0000000 rate_onset
## 19 1.0000000 rate_decline
We see here that the windowHalfWidth
has a large effect on the difference between the results than anything else so far. The difference per row is still low at ~0.001 so isn’t much cause for alarm. We do however detect a different number of events if windowHalfWidth = 7
. Let’s see how they differ.
whw_7_r_event <- whw_7_r$event
whw_7_r_event$date_start[!whw_7_r_event$date_start %in% whw_7_py_event$date_start]
## [1] "2006-03-05"
The difference lies in the R code detecting a five day event that the Python code must have seen as only 4 days or less. Again due to very narrow differences in the threshold values.
Let’s look if there is a difference between the old and new R code.
ts <- RmarineHeatWaves::make_whole(sst_WA)
whw_7_r_old <- RmarineHeatWaves::detect(data = ts, climatology_start = "1982-01-01", climatology_end = "2014-12-31", window_half_width = 7)
# Compare new and old R code
sum(whw_7_r$climatology$seas) - sum(whw_7_r_old$clim$seas_clim_year)
## [1] -0.06257652
## [1] 0.003332891
## [1] 0.004781785
## [1] 0.0001258996
Everything matches up between old and new R for windowHalfWidth = 7
.
minDuration
comparisonsNow we move away form the arguments that effect the climatology calculations, and focus just on those that determine the classification of events.
minD_3_r <- detect_event(ts2clm(data = sst_WA, climatologyPeriod = c("1982-01-01", "2014-12-31")),
minDuration = 3)
minD_7_r <- detect_event(ts2clm(data = sst_WA, climatologyPeriod = c("1982-01-01", "2014-12-31")),
minDuration = 7)
# minDuration = 3
mhws, clim = mhw.detect(t, sst, minDuration = 3)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_minD_3.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_minD_3.csv', sep = ',', index = False)
# minDuration = 7
mhws, clim = mhw.detect(t, sst, minDuration = 7)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_minD_7.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_minD_7.csv', sep = ',', index = False)
# Load Python
minD_3_py_event <- read_csv("data/mhws_py_minD_3.csv")
minD_3_py_clim <- read_csv("data/clim_py_minD_3.csv")
minD_7_py_event <- read_csv("data/mhws_py_minD_7.csv")
minD_7_py_clim <- read_csv("data/clim_py_minD_7.csv")
# Compare clims and intensities
# 3 days
cor(minD_3_r$climatology$seas, minD_3_py_clim$seas)
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999996
## [1] -6.573385
## [1] 0.1969908
## [1] 0.01369288
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999996
## [1] -6.573385
## [1] 0.1169319
## [1] 0.007283979
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999988 intensity_mean
## 7 0.9999997 intensity_max
## 8 0.9987979 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999895 intensity_mean_relThresh
## 11 0.9999946 intensity_max_relThresh
## 12 0.9987109 intensity_var_relThresh
## 13 0.9999996 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9991713 intensity_max_abs
## 16 0.9990268 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 1.0000000 rate_onset
## 19 0.9999999 rate_decline
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999992 intensity_mean
## 7 0.9999997 intensity_max
## 8 0.9992868 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999965 intensity_mean_relThresh
## 11 0.9999768 intensity_max_relThresh
## 12 0.9992416 intensity_var_relThresh
## 13 0.9999995 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9996425 intensity_max_abs
## 16 0.9994579 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999999 rate_onset
## 19 0.9999999 rate_decline
As expected, changing the minimum duration of events has no effect on the threshold calculations. It is however gratifying to see that the same number of events is calculated between languages, regardless of the minimum duration. The difference these cause in the intensities of the events is also negligible between languages.
joinAcrossGaps
comparisonsNow for the final argument in the base functionality. This too should not effect the climatologies. Only the events.
joinAG_no_r <- detect_event(ts2clm(data = sst_WA, climatologyPeriod = c("1982-01-01", "2014-12-31")),
joinAcrossGaps = FALSE)
joinAG_1_r <- detect_event(ts2clm(data = sst_WA, climatologyPeriod = c("1982-01-01", "2014-12-31")),
maxGap = 1)
joinAG_5_r <- detect_event(ts2clm(data = sst_WA, climatologyPeriod = c("1982-01-01", "2014-12-31")),
maxGap = 5)
# joinAGuration = False
mhws, clim = mhw.detect(t, sst, joinAcrossGaps = False)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_joinAG_no.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_joinAG_no.csv', sep = ',', index = False)
# maxGap = 1
mhws, clim = mhw.detect(t, sst, maxGap = 1)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_joinAG_1.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_joinAG_1.csv', sep = ',', index = False)
# maxGap = 5
mhws, clim = mhw.detect(t, sst, maxGap = 5)
mhws_df = pd.DataFrame.from_dict(mhws)
mhws_df.to_csv('data/mhws_py_joinAG_5.csv', sep = ',', index = False)
clim_df = pd.DataFrame.from_dict(clim)
clim_df.to_csv('data/clim_py_joinAG_5.csv', sep = ',', index = False)
# Load Python
joinAG_no_py_event <- read_csv("data/mhws_py_joinAG_no.csv")
joinAG_no_py_clim <- read_csv("data/clim_py_joinAG_no.csv")
joinAG_1_py_event <- read_csv("data/mhws_py_joinAG_1.csv")
joinAG_1_py_clim <- read_csv("data/clim_py_joinAG_1.csv")
joinAG_5_py_event <- read_csv("data/mhws_py_joinAG_5.csv")
joinAG_5_py_clim <- read_csv("data/clim_py_joinAG_5.csv")
# Compare clims and intensities
# no days
cor(joinAG_no_r$climatology$seas, joinAG_no_py_clim$seas)
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999996
## [1] -6.573385
## [1] 0.1559537
## [1] 0.01446085
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999996
## [1] -6.573385
## [1] 0.1584107
## [1] 0.01211662
## [1] 0.9999999
## [1] -2.053821
## [1] 0.9999996
## [1] -6.573385
## [1] 0.1610846
## [1] 0.008638111
# Compare events
joinAG_no_event <- compare_event(joinAG_no_r$event, joinAG_no_py_event)
joinAG_no_event
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999989 intensity_mean
## 7 0.9999996 intensity_max
## 8 0.9989905 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999973 intensity_mean_relThresh
## 11 0.9999993 intensity_max_relThresh
## 12 0.9989440 intensity_var_relThresh
## 13 0.9999996 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9997855 intensity_max_abs
## 16 0.9992315 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999999 rate_onset
## 19 0.9999999 rate_decline
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999991 intensity_mean
## 7 0.9999997 intensity_max
## 8 0.9992930 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999971 intensity_mean_relThresh
## 11 0.9999832 intensity_max_relThresh
## 12 0.9992473 intensity_var_relThresh
## 13 0.9999996 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9997788 intensity_max_abs
## 16 0.9995174 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999999 rate_onset
## 19 0.9999999 rate_decline
## r var
## 1 NA event_no
## 2 1.0000000 index_start
## 3 1.0000000 index_peak
## 4 1.0000000 index_end
## 5 1.0000000 duration
## 6 0.9999990 intensity_mean
## 7 0.9999997 intensity_max
## 8 0.9995542 intensity_var
## 9 1.0000000 intensity_cumulative
## 10 0.9999960 intensity_mean_relThresh
## 11 0.9999993 intensity_max_relThresh
## 12 0.9995227 intensity_var_relThresh
## 13 0.9999995 intensity_cumulative_relThresh
## 14 1.0000000 intensity_mean_abs
## 15 0.9966753 intensity_max_abs
## 16 0.9997826 intensity_var_abs
## 17 1.0000000 intensity_cumulative_abs
## 18 0.9999999 rate_onset
## 19 0.9999999 rate_decline
A little surprising, but good to see, is that the length of gap between events does not cause a different number of events to be calculated between languages considering all other arguments are kept at the default.
It appears as though, down to a very small degree, there are differences between the outputs of the two languages. That being said, these differences appear to be due to the rounding differences that exist between the languages and so it is not a fault of the code in either, nor are there fundamentally different processes being employed to make the calculations. Indeed, though minute differences may be found in the calculations of the seasonal and threshold climatologies, these differences are consistent. They do not vary. They may change in magnitude depending on the arguments supplied, but these differences never become larger than ~0.001 per row, which has been deemed here as acceptable. Interestingly, though not surprising in hindsight, is that the changing of the windowHalfWidth
argument causes the largest difference between the languages. This makes sense because this argument determines how many values are meaned together in the beginning of the climatology calculations. And as the differences are due to rounding, that numerical fingerprint is boldest here.
Depending on how some of the arguments were tweaked, the two languages very rarely would detect one event more or less than the other. This was due to the odd day being just above or below the threshold by ~0.001 degrees or so. Very rare, but so is existence, hypothetically.
Ultimately, I am very confident in saying that the outputs of these two languages are comparable.