<!DOCTYPE html> <!-- Generated by pkgdown: do not edit by hand --><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Additional functionality for MHW outputs in R and Python • MHWdetection</title> <!-- jquery --><script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha384-nrOSfDHtoPMzJHjVTdCopGqIqeYETSXhZDFyniQ8ZHcVy08QesyHcnOUpMpqnmWq" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script><!-- Font Awesome icons --><link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous"> <!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js" integrity="sha384-cV+rhyOuRHc9Ub/91rihWcGmMmCXDeksTtCihMupQHSsi8GIIRDG0ThDc3HGQFJ3" crossorigin="anonymous"></script><!-- sticky kit --><script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-kit/1.1.3/sticky-kit.min.js" integrity="sha256-c4Rlo1ZozqTPE2RLuvbusY3+SU1pQaJC0TjuhygMipw=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet"> <script src="../pkgdown.js"></script><meta property="og:title" content="Additional functionality for MHW outputs in R and Python"> <meta property="og:description" content="This vignette compares the code/work flow for altering the additional arguments found within the base Python and R detection function."> <meta name="twitter:card" content="summary"> <!-- mathjax --><script src="https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <div class="container template-article"> <header><div class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <span class="navbar-brand"> <a class="navbar-link" href="../index.html">MHWdetection</a> <span class="label label-default" data-toggle="tooltip" data-placement="bottom" title="Released package">0.0.3</span> </span> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li> <a href="../index.html"> <span class="fa fa-home fa-lg"></span> </a> </li> <li> <a href="../reference/index.html">Functions</a> </li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> Vignettes <span class="caret"></span> </a> <ul class="dropdown-menu" role="menu"> <li> <a href="../articles/r_vs_python.html">Python vs. R - default outputs</a> </li> <li> <a href="../articles/r_vs_python_arguments.html">Python vs. R - default arguments</a> </li> <li> <a href="../articles/r_vs_python_additional.html">Python vs. R - additional functions</a> </li> <li> <a href="../articles/Short_climatologies.html">Short climatologies</a> </li> <li> <a href="../articles/Climatologies_and_baselines.html">Alternative climatologies and baselines</a> </li> <li> <a href="../articles/time_series_duration.html">Effects of short time series</a> </li> <li> <a href="../articles/missing_data.html">Effects of missing data</a> </li> <li> <a href="../articles/gridded_products.html">Difference between gridded products</a> </li> <li> <a href="../articles/best_practices.html">Best practices</a> </li> </ul> </li> <li> <a href="../news/index.html">News</a> </li> </ul> <ul class="nav navbar-nav navbar-right"> <li> <a href="https://github.com/robwschlegel/MHWdetection"> <span class="fa fa-github fa-lg"></span> github </a> </li> </ul> </div> <!--/.nav-collapse --> </div> <!--/.container --> </div> <!--/.navbar --> </header><div class="row"> <div class="col-md-9 contents"> <div class="page-header toc-ignore"> <h1>Additional functionality for MHW outputs in R and Python</h1> <h4 class="author">Robert W Schlegel</h4> <h4 class="date">2018-09-13</h4> <div class="hidden name"><code>r_vs_python_additional.Rmd</code></div> </div> <div id="overview" class="section level2"> <h2 class="hasAnchor"> <a href="#overview" class="anchor"></a>Overview</h2> <p>In this last comparison vignette between the two languages we will look at the other options that exist within the two languages. Slightly different approaches have been taken to provide the user with the ability to calculate block averages from the detection outputs as well as categorising the ‘extremeness’ of events. We will compare these different workflows/outputs below.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(tidyverse) <span class="kw">library</span>(ggpubr) <span class="kw">library</span>(heatwaveR)</code></pre></div> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">compare_event <-<span class="st"> </span><span class="cf">function</span>(res_event_R, res_event_Python){ <span class="co"># Remove non-numeric columns</span> res_event_num <-<span class="st"> </span>res_event_R <span class="op">%>%</span><span class="st"> </span> <span class="st"> </span><span class="kw">select_if</span>(is.numeric) <span class="co"># Run the loop</span> res_event <-<span class="st"> </span><span class="kw">data.frame</span>() <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span><span class="kw">length</span>(<span class="kw">colnames</span>(res_event_num))){ <span class="cf">if</span>(<span class="kw">colnames</span>(res_event_num)[i] <span class="op">%in%</span><span class="st"> </span><span class="kw">colnames</span>(res_event_Python)){ x1 <-<span class="st"> </span>res_event_R[<span class="kw">colnames</span>(res_event_R) <span class="op">==</span><span class="st"> </span><span class="kw">colnames</span>(res_event_num)[i]] x2 <-<span class="st"> </span>res_event_Python[<span class="kw">colnames</span>(res_event_Python) <span class="op">==</span><span class="st"> </span><span class="kw">colnames</span>(res_event_num)[i]] x <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">r =</span> <span class="kw">cor</span>(x1, x2, <span class="dt">use =</span> <span class="st">"complete.obs"</span>), <span class="dt">sum =</span> <span class="kw">sum</span>(x1, <span class="dt">na.rm =</span> T) <span class="op">-</span><span class="st"> </span><span class="kw">sum</span>(x2, <span class="dt">na.rm =</span> T), <span class="dt">var =</span> <span class="kw">colnames</span>(res_event_num)[i]) <span class="kw">colnames</span>(x)[<span class="dv">1</span>] <-<span class="st"> "r"</span> <span class="kw">rownames</span>(x) <-<span class="st"> </span><span class="ot">NULL</span> } <span class="cf">else</span> { x <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">r =</span> <span class="ot">NA</span>, <span class="dt">sum =</span> <span class="ot">NA</span>, <span class="dt">var =</span> <span class="kw">colnames</span>(res_event_num)[i]) } res_event <-<span class="st"> </span><span class="kw">rbind</span>(res_event, x) } <span class="kw">return</span>(res_event) }</code></pre></div> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(reticulate) <span class="kw"><a href="http://www.rdocumentation.org/packages/reticulate/topics/use_python">use_condaenv</a></span>(<span class="st">"py27"</span>)</code></pre></div> <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> numpy <span class="im">as</span> np <span class="im">from</span> datetime <span class="im">import</span> date <span class="im">import</span> pandas <span class="im">as</span> pd <span class="im">import</span> marineHeatWaves <span class="im">as</span> mhw <span class="co"># The date values</span> t <span class="op">=</span> np.arange(date(<span class="dv">1982</span>,<span class="dv">1</span>,<span class="dv">1</span>).toordinal(),date(<span class="dv">2014</span>,<span class="dv">12</span>,<span class="dv">31</span>).toordinal()<span class="op">+</span><span class="dv">1</span>) <span class="co"># The temperature values</span> sst <span class="op">=</span> np.loadtxt(<span class="bu">open</span>(<span class="st">"data/sst_WA.csv"</span>, <span class="st">"r"</span>), delimiter <span class="op">=</span> <span class="st">','</span>, skiprows <span class="op">=</span> <span class="dv">1</span>) <span class="co"># The event metrics</span> mhws, clim <span class="op">=</span> mhw.detect(t, sst)</code></pre></div> </div> <div id="block_average-comparisons" class="section level2"> <h2 class="hasAnchor"> <a href="#block_average-comparisons" class="anchor"></a><code><a href="http://www.rdocumentation.org/packages/heatwaveR/topics/block_average">block_average()</a></code> comparisons</h2> <p>First up we take a peak at the <code><a href="http://www.rdocumentation.org/packages/heatwaveR/topics/block_average">block_average()</a></code> functions and the outputs they produce.</p> <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">mhwBlock <span class="op">=</span> mhw.blockAverage(t, mhws, clim) mhwsBlock_df <span class="op">=</span> pd.DataFrame.from_dict(mhwBlock) mhwsBlock_df.to_csv(<span class="st">'data/mhwBlock.csv'</span>, sep <span class="op">=</span> <span class="st">','</span>, index <span class="op">=</span> <span class="va">False</span>)</code></pre></div> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">default_r <-<span class="st"> </span><span class="kw"><a href="http://www.rdocumentation.org/packages/heatwaveR/topics/detect_event">detect_event</a></span>(<span class="kw"><a href="http://www.rdocumentation.org/packages/heatwaveR/topics/ts2clm">ts2clm</a></span>(<span class="dt">data =</span> sst_WA, <span class="dt">climatologyPeriod =</span> <span class="kw">c</span>(<span class="st">"1982-01-01"</span>, <span class="st">"2014-12-31"</span>))) block_r <-<span class="st"> </span><span class="kw"><a href="http://www.rdocumentation.org/packages/heatwaveR/topics/block_average">block_average</a></span>(default_r) block_py <-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">"data/mhwBlock.csv"</span>)</code></pre></div> <div id="overlapping-column-names" class="section level3"> <h3 class="hasAnchor"> <a href="#overlapping-column-names" class="anchor"></a>Overlapping column names</h3> <p>Initially we want to see how well the naming conventions for the columns hold up.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cols_r <-<span class="st"> </span><span class="kw">colnames</span>(block_r)[<span class="op">!</span>(<span class="kw">colnames</span>(block_r) <span class="op">%in%</span><span class="st"> </span><span class="kw">colnames</span>(block_py))] cols_r</code></pre></div> <pre><code>## [1] "year"</code></pre> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cols_py <-<span class="st"> </span><span class="kw">colnames</span>(block_py)[<span class="op">!</span>(<span class="kw">colnames</span>(block_py) <span class="op">%in%</span><span class="st"> </span><span class="kw">colnames</span>(block_r))] cols_py</code></pre></div> <pre><code>## [1] "years_centre" "years_end" "years_start"</code></pre> <p>The one column that the R code outputs that the Python code lacks is <code>year</code>. We may see that the Python code creates <code>years_centre</code>, <code>years_end</code>, and <code>years_start</code>. The difference for this is that the Python version is set up to allow for block averages to be calculated for units of time other than single years, whereas the R version only outputs single annual means. I don’t know that this needs to be expanded upon in the R code. I don’t see this functions as terribly useful, to be honest. Surely if a user has gotten to this point, they can calculate block averages on their own to their own preferences.</p> </div> <div id="comparison-of-outputs" class="section level3"> <h3 class="hasAnchor"> <a href="#comparison-of-outputs" class="anchor"></a>Comparison of outputs</h3> <p>Up next we look at how well the outputs correlate and sum up. As we saw in the previous vignette, correlation values are useful for showing how consistently similar the results are, but not for if they are exact. For that we will compare sums as well.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">compare_event</span>(block_r, block_py)</code></pre></div> <pre><code>## r sum var ## 1 NA NA year ## 2 1.0000000 0.000000e+00 count ## 3 1.0000000 0.000000e+00 duration ## 4 0.9999991 3.213840e-03 intensity_mean ## 5 0.9999995 3.822194e-03 intensity_max ## 6 0.9999997 6.304469e-03 intensity_max_max ## 7 0.9992296 2.350236e-01 intensity_var ## 8 1.0000000 5.376411e-02 intensity_cumulative ## 9 0.9999986 4.120556e-03 intensity_mean_relThresh ## 10 0.9999996 4.575263e-03 intensity_max_relThresh ## 11 0.9991691 2.341655e-01 intensity_var_relThresh ## 12 0.9999999 1.092277e-01 intensity_cumulative_relThresh ## 13 1.0000000 0.000000e+00 intensity_mean_abs ## 14 0.9998088 3.368333e-01 intensity_max_abs ## 15 0.9998059 2.489592e-01 intensity_var_abs ## 16 1.0000000 0.000000e+00 intensity_cumulative_abs ## 17 1.0000000 -3.197191e-05 rate_onset ## 18 0.9999997 2.187176e-04 rate_decline ## 19 0.9683879 0.000000e+00 total_days ## 20 0.9600666 1.615886e-01 total_icum</code></pre> <p>As expected, the results correlate and sum up to nearly identical values. The mismatch being a product of rounding differences between the languages.</p> </div> </div> <div id="trend-comparisons" class="section level2"> <h2 class="hasAnchor"> <a href="#trend-comparisons" class="anchor"></a>Trend comparisons</h2> <p>There is no comparable R functions that performs these calculations. One could be created if the desire exists.</p> <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">mean, trend, dtrend <span class="op">=</span> mhw.meanTrend(mhwBlock) <span class="co">#print(mean)</span> <span class="co">#print(trend)</span> <span class="co">#print(dtrend)</span></code></pre></div> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Print out results in R as desired</span> <span class="co">#py$mean</span> <span class="co">#py$trend</span> <span class="co">#py$dtrend</span></code></pre></div> </div> <div id="category-comparisons" class="section level2"> <h2 class="hasAnchor"> <a href="#category-comparisons" class="anchor"></a>Category comparisons</h2> <p>The final bit of extra functionality that must be compared between the two languages is the newest addition for both. This is the calculation of categories for MHWs as seen in Hobday et al. (2018). The two languages go about the calculation of these events in rather different ways. And produce different outputs. This is however intentional and so it must be decided if this is to be made more consistent, or be left as it is.</p> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Load Python category results</span> category_py <-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">"data/mhws_py.csv"</span>) <span class="op">%>%</span><span class="st"> </span> <span class="st"> </span><span class="kw">select</span>(date_peak, category, intensity_max, duration, duration_moderate, duration_strong, duration_severe, duration_extreme) category_py</code></pre></div> <pre><code>## # A tibble: 60 x 8 ## date_peak category intensity_max duration duration_modera… ## <date> <chr> <dbl> <int> <int> ## 1 1984-06-05 Moderate 1.98 12 12 ## 2 1984-06-19 Moderate 2.13 6 6 ## 3 1984-07-10 Moderate 2.24 19 19 ## 4 1984-10-06 Moderate 1.29 5 5 ## 5 1984-10-23 Moderate 1.83 7 7 ## 6 1984-10-30 Moderate 1.50 20 17 ## 7 1985-07-17 Moderate 2.21 7 7 ## 8 1987-10-04 Moderate 1.12 5 5 ## 9 1988-06-11 Moderate 1.69 6 6 ## 10 1988-06-28 Moderate 1.99 6 6 ## # ... with 50 more rows, and 3 more variables: duration_strong <int>, ## # duration_severe <int>, duration_extreme <int></code></pre> <div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Calculate categories in R</span> category_r <-<span class="st"> </span><span class="kw"><a href="http://www.rdocumentation.org/packages/heatwaveR/topics/category">category</a></span>(default_r, <span class="dt">name =</span> <span class="st">"WA"</span>) category_r</code></pre></div> <pre><code>## # A tibble: 60 x 11 ## event_no event_name peak_date category i_max duration p_moderate ## <int> <fct> <date> <chr> <dbl> <int> <dbl> ## 1 1 <NA> 1984-06-05 I Moder… 1.98 12 100 ## 2 2 <NA> 1984-06-19 I Moder… 2.13 6 100 ## 3 3 <NA> 1984-07-10 I Moder… 2.24 19 100 ## 4 4 <NA> 1984-10-06 I Moder… 1.29 5 100 ## 5 5 <NA> 1984-10-23 I Moder… 1.83 7 100 ## 6 7 <NA> 1985-07-17 I Moder… 2.21 7 100 ## 7 8 <NA> 1987-10-04 I Moder… 1.12 5 100 ## 8 9 <NA> 1988-06-11 I Moder… 1.69 6 100 ## 9 10 <NA> 1988-06-28 I Moder… 1.99 6 100 ## 10 11 <NA> 1988-10-25 I Moder… 1.55 11 100 ## # ... with 50 more rows, and 4 more variables: p_strong <dbl>, ## # p_severe <dbl>, p_extreme <dbl>, season <chr></code></pre> <p>I won’t go about comparing the outputs of these functions the same as I have for the other functions because, as stated above, they show different information. The R output is specifically tailored to match the information style of Table 2 in Hobday et al. (2018). This is one final point on which a decision must be made about whether or not the extra functionality of the languages should be brought to be the exact same, or allowed to differ. I think it is fine the way it is now.</p> </div> <div id="conclusion" class="section level2"> <h2 class="hasAnchor"> <a href="#conclusion" class="anchor"></a>Conclusion</h2> <p>Two questions still beg an answer:</p> <ol style="list-style-type: decimal"> <li>Do we want to have a built in trend detecting function in the R code, <em>a la</em> <code>meanTrend</code> in the Python package? I don’t think so. We could write a vignette in the <code>heatwaveR</code> site showing users how to perform this calculation themselves, but I don’t think it needs to be included as a function. It would be simple to do should the desire exist.</li> <li>The output of the category information between the two languages is quite different. The Python code provides, as part of the base <code>detect</code> output, the days spent within each category, as well as the maximum category reached. The R output rather provides the proportion of time spent in each category, as well as the maximum category. The R code has additional columns added in to better match Table 2 in Hobday et al. (2018). I think this is useful as a user would probably want a summary output of the category information. But I could be convinced that it is the days, rather than proportions, that should be provided without the additional columns as well.</li> </ol> <p>That wraps up the comparisons of the languages. It can be said that where small rounding differences persist between the languages, the base outputs are comparable and the languages may be used interchangeably. The extra functionality also matches up, minus a couple of issues I will address. There are some difference, but these are stylistic and it is not clear that they should be changed/addressed.</p> <p>It is now time to get started on looking at how to be go about consistently and reliably detecting thermal events in time series given a range of potential sub-optimal conditions.</p> </div> </div> <div class="col-md-3 hidden-xs hidden-sm" id="sidebar"> <div id="tocnav"> <h2 class="hasAnchor"> <a href="#tocnav" class="anchor"></a>Contents</h2> <ul class="nav nav-pills nav-stacked"> <li><a href="#overview">Overview</a></li> <li><a href="#block_average-comparisons"><code><a href="http://www-rdocumentation-org/packages/heatwaveR/topics/block_average">block_average()</a></code> comparisons</a></li> <li><a href="#trend-comparisons">Trend comparisons</a></li> <li><a href="#category-comparisons">Category comparisons</a></li> <li><a href="#conclusion">Conclusion</a></li> </ul> </div> </div> </div> <footer><div class="copyright"> <p>Developed by Robert W. Schlegel, Eric C. J. Oliver, Alistair J. Hobday, Albertus J. Smit.</p> </div> <div class="pkgdown"> <p>Site built with <a href="http://pkgdown.r-lib.org/">pkgdown</a>.</p> </div> </footer> </div> </body> </html>