From 48695bd47b85949c9caba61063b0b7d855bc8020 Mon Sep 17 00:00:00 2001 From: Nicholas Long Date: Fri, 25 Apr 2025 17:10:14 -0600 Subject: [PATCH 1/2] bump to rnoaa 1.4.0 --- DESCRIPTION | 3 ++- README.md | 10 ++++++++++ setup_environment.R | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8d47105..6e02e0f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,8 +10,9 @@ License: See LICENSE.txt Encoding: UTF-8 LazyData: true Imports: - xml2 (>= 1.3.2), + xml2 (>= 1.3.8), nmecr (>= 1.0.17), + rnoaa (>= 1.4.0), anytime RoxygenNote: 7.1.0 Suggests: diff --git a/README.md b/README.md index 12ae01e..7416b8e 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,19 @@ library(bsyncr) # example on using. ``` +## Testing + +Tests are automatically run on GitHub. To run locally, make sure to have R installed along with various dev packages. + +```bash +Rscript setup_environment.R +Rscript -e "testthat::test_dir('tests')" +``` + ## Releasing new version - Create a branch with the prepared release change log. +- Make sure the rnoaa and nmecr versions in `setup_environment.R` and ` are correct. - Update version in bsync.RProj and DESCRIPTION to the next correct semantic version - Make sure the DESCRIPTION has the correct version of the dependencies - For testing purposes, make sure the versions of NMECR and RNOAA are correct in the `setup_environment.R` script diff --git a/setup_environment.R b/setup_environment.R index 9c76b86..53000d8 100644 --- a/setup_environment.R +++ b/setup_environment.R @@ -20,7 +20,7 @@ for (pkg in required_packages) { # Install specific versions of GitHub packages cat("Installing GitHub packages...\n") -remotes::install_github("ropensci/rnoaa@v1.3.4", upgrade = "never") +remotes::install_github("ropensci/rnoaa@v1.4.0", upgrade = "never") remotes::install_github("kW-Labs/nmecr@v1.0.17", upgrade = "never") # Populate NOAA stations (required for rnoaa) From fa152431009b74319112b07049e2a2bde96bfee4 Mon Sep 17 00:00:00 2001 From: Nicholas Long Date: Sat, 26 Apr 2025 09:56:23 -0600 Subject: [PATCH 2/2] add in the lineal regression for SLR --- .gitignore | 8 ++++ README.md | 8 ++++ bsyncr.Rproj | 3 +- setup_environment.R | 3 +- tests/bsyncr_example.Rmd | 85 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/bsyncr_example.Rmd diff --git a/.gitignore b/.gitignore index 1b0f9bf..36c8d8c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,16 @@ .DS_Store .python-version +.Renviron +.Rhistory +.RData node_modules +tests/*.zip +tests/*.nb.html +tests/*_results/ + vignettes/*.html vignettes/*.R vignettes/output +.Rproj.user diff --git a/README.md b/README.md index 7416b8e..b2ab81f 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ Rscript -e "testthat::test_dir('tests')" ## Releasing new version +- Open `bysync.Rproj` in RStudio +- In RStudio, format all the R files by running the following commands in RStudio + +```R +install.packages("styler") +styler::style_dir() +``` + - Create a branch with the prepared release change log. - Make sure the rnoaa and nmecr versions in `setup_environment.R` and ` are correct. - Update version in bsync.RProj and DESCRIPTION to the next correct semantic version diff --git a/bsyncr.Rproj b/bsyncr.Rproj index fb44373..a1aa64e 100644 --- a/bsyncr.Rproj +++ b/bsyncr.Rproj @@ -1,4 +1,5 @@ -Version: 0.1 +Version: 1.0 +ProjectId: b4573bbf-3288-46a6-89f8-fe2dc7103016 RestoreWorkspace: Default SaveWorkspace: Default diff --git a/setup_environment.R b/setup_environment.R index 53000d8..672cc08 100644 --- a/setup_environment.R +++ b/setup_environment.R @@ -7,8 +7,7 @@ cat(getwd(), "\n\n") # Install required packages if not already installed required_packages <- c( - "remotes", "crayon", "dplyr", "tidyr", "crul", "xml2", "testthat", "anytime", "lubridate", "segmented", "xts", "zoo", - "ggplot2", "scales", "XML", "rappdirs", "gridExtra", "isdparser", "geonames", "hoardr", "data.table" + "remotes", "crayon", "dplyr", "tidyr", "crul", "xml2", "testthat", "anytime", "lubridate", "segmented", "xts", "zoo", "ggplot2", "scales", "XML", "rappdirs", "gridExtra", "isdparser", "geonames", "hoardr", "data.table" ) cat("Checking and installing required packages...\n") diff --git a/tests/bsyncr_example.Rmd b/tests/bsyncr_example.Rmd new file mode 100644 index 0000000..e4d6cd1 --- /dev/null +++ b/tests/bsyncr_example.Rmd @@ -0,0 +1,85 @@ +--- +title: "bsyncr Functionality Demonstration" +output: html_notebook +--- + +Create a .Renviron file in the project folder and set your NOAA_TOKEN + +e.g., + +NOAA_TOKEN=xyz123 + + +```{r setup, include=FALSE} +# set the working directory to the project folder, not the location of this file. +setwd("..") + +# install the dependencies +source("./setup_environment.R") + +# Load required libraries +library(xml2) +library(rnoaa) +library(lubridate) +library(dplyr) +library(ggplot2) + + +# Source the utility functions +source("./R/bsync_utils.R") + +# get root path +root_path <- getwd() + +# Ensure NOAA token is set +NOAA_TOKEN <- Sys.getenv('NOAA_TOKEN') +if (NOAA_TOKEN == "") { + stop("Missing NOAA token env var: NOAA_TOKEN") +} +options(noaakey = NOAA_TOKEN) +``` + +```{r} + +# Path to the test file +# bsync path from root path +bsync_filepath <- file.path(root_path, "tests", "data", "ex_bsync.xml") + + +baseline_scenario_id <- "Scenario-bsyncr" +bsync_doc <- xml2::read_xml(bsync_filepath) %>% + bsyncr::bs_stub_scenarios(linked_building_id = "My-Fav-Building", baseline_id = baseline_scenario_id) + +baseline_xpath <- sprintf("//auc:Scenario[@ID = '%s']", baseline_scenario_id) +sc_baseline <- xml2::xml_find_first(bsync_doc, baseline_xpath) +not_used <- sc_baseline %>% bsyncr::bs_stub_derived_model(dm_id = "DerivedModel-bsyncr", + dm_period = "Baseline") + +b_df <- bsyncr::bs_parse_nmecr_df(bsync_doc, insert_weather_data = TRUE) +``` + +```{r} +# Create an SLR model +model <- nmecr::model_with_SLR(b_df, nmecr::assign_model_inputs(regression_type = "SLR")) + +model_df <- model$training_data %>% + tidyr::gather(key = "variable", value = "value", c("eload", "model_fit")) + +print(model_df) + +# add in the linear regression line from the model results, need to +# confirm, but it looks like model is in BTU and °C +intercept = model$model$coefficients[["(Intercept)"]] / 3.41214 # btu to kwh +# Model is in °C, so convert to F. +slope = model$model$coefficients[["temp"]] * 9/5 # °C to °F + +ggplot2::ggplot(model_df, aes(x = temp, y = value)) + + geom_point(aes(color = variable), data=model_df[model_df$variable == "eload",]) + + geom_line(aes(color = variable), data=model_df[model_df$variable == "model_fit",]) + + geom_abline(intercept = intercept, slope = slope, color = "red", linetype = "dashed") + + xlab("Temperature") + + scale_y_continuous(name = "Energy Data & Model Fit (kWh)", labels = scales::comma) + + theme_minimal() + + theme(legend.position = "bottom") + + theme(legend.title = element_blank()) +```