From ec79637749d300ab4ca170805c673905e52d67dd Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 22 Apr 2015 13:42:10 +0200 Subject: Add simplest PEC soil calcs, use testthat --- GNUmakefile | 63 ++++++++++++++++++++++++++ Makefile | 51 --------------------- README.rmd | 16 ++++++- pkg/DESCRIPTION | 11 +++-- pkg/NAMESPACE | 3 +- pkg/R/PEC_soil.R | 45 ++++++++++++++++++ pkg/man/PEC_soil.Rd | 36 +++++++++++++++ pkg/man/TOXSWA_cwa.Rd | 2 +- pkg/man/plot.TOXSWA_cwa.Rd | 2 +- pkg/man/read.TOXSWA_cwa.Rd | 2 +- pkg/tests/testthat.R | 4 ++ pkg/tests/testthat/H_sw_D4_pond.rds | Bin 0 -> 243113 bytes pkg/tests/testthat/H_sw_R1_stream_events.rds | Bin 0 -> 332 bytes pkg/tests/testthat/H_sw_R1_stream_windows.rds | Bin 0 -> 234 bytes pkg/tests/testthat/test_PEC_soil.R | 13 ++++++ pkg/tests/testthat/test_TOXSWA_cwa.R | 25 ++++++++++ 16 files changed, 213 insertions(+), 60 deletions(-) create mode 100644 GNUmakefile delete mode 100644 Makefile create mode 100644 pkg/R/PEC_soil.R create mode 100644 pkg/man/PEC_soil.Rd create mode 100644 pkg/tests/testthat.R create mode 100644 pkg/tests/testthat/H_sw_D4_pond.rds create mode 100644 pkg/tests/testthat/H_sw_R1_stream_events.rds create mode 100644 pkg/tests/testthat/H_sw_R1_stream_windows.rds create mode 100644 pkg/tests/testthat/test_PEC_soil.R create mode 100644 pkg/tests/testthat/test_TOXSWA_cwa.R diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 0000000..93708a6 --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,63 @@ +PKGSRC := $(shell basename $(CURDIR)) +PKGNAME := $(shell sed -n "s/Package: *\([^ ]*\)/\1/p" pkg/DESCRIPTION) +PKGVERS := $(shell sed -n "s/Version: *\([^ ]*\)/\1/p" pkg/DESCRIPTION) +TGZ := $(PKGSRC)_$(PKGVERS).tar.gz +R_HOME ?= $(shell R RHOME) +DATE := $(shell date +%Y-%m-%d) + +.PHONEY: usage check clean + +usage: + @echo "Usage: make TARGET with TARGET being:" + @echo "" + @echo " clean - Clean up." + @echo " roxygen - Roxygenize." + @echo " build - Build source package." + @echo " check - Run CRAN check on the package." + @echo " install - Install the package." + +pkgfiles = pkg/DESCRIPTION \ + pkg/inst/testdata/* \ + pkg/tests/testthat.R \ + pkg/tests/testthat/* \ + pkg/R/* + +clean: + @echo "Cleaning up..." + rm -fR pkg.Rcheck + @echo "DONE." + +roxygen: + @echo "Roxygenizing package..." + "$(R_HOME)/bin/Rscript" -e 'library(roxygen2); roxygenize("pkg")' > roxygen.log 2>&1 || cat roxygen.log + @echo "DONE." + +$(TGZ): $(pkgfiles) + @echo "Building package..." + sed -i -e "s/Date:.*/Date: $(DATE)/" pkg/DESCRIPTION + "$(R_HOME)/bin/Rscript" -e 'library(roxygen2); roxygenize("pkg")' 2>&1 | tee roxygen.log + git log --no-merges -M --date=iso pkg/ > pkg/ChangeLog + "$(R_HOME)/bin/R" CMD build pkg > build.log 2>&1 + @echo "DONE." + +build: $(TGZ) + +test: build + @echo "Running testthat tests..." + "$(R_HOME)/bin/Rscript" -e 'library(devtools); devtools::test("pkg")' 2>&1 | tee roxygen.log + @echo "DONE." + +check: build + @echo "Running check..." + "$(R_HOME)/bin/R" CMD check $(TGZ) + @echo "DONE." + +crancheck: build + @echo "Running CRAN check..." + "$(R_HOME)/bin/R" CMD check --as-cran $(TGZ) + @echo "DONE." + +install: build + @echo "Installing package..." + "$(R_HOME)/bin/R" CMD INSTALL --no-multiarch $(TGZ) + @echo "DONE." diff --git a/Makefile b/Makefile deleted file mode 100644 index db1ade6..0000000 --- a/Makefile +++ /dev/null @@ -1,51 +0,0 @@ -PKGSRC := $(shell basename $(CURDIR)) -PKGNAME := $(shell sed -n "s/Package: *\([^ ]*\)/\1/p" pkg/DESCRIPTION) -PKGVERS := $(shell sed -n "s/Version: *\([^ ]*\)/\1/p" pkg/DESCRIPTION) -TGZ := $(PKGSRC)_$(PKGVERS).tar.gz -R_HOME ?= $(shell R RHOME) -DATE := $(shell date +%Y-%m-%d) - -.PHONEY: usage check clean - -usage: - @echo "Usage: make TARGET with TARGET being:" - @echo "" - @echo " clean - Clean up." - @echo " roxygen - Roxygenize." - @echo " build - Build source package." - @echo " check - Run CRAN check on the package." - @echo " install - Install the package." - -pkgfiles = pkg/DESCRIPTION \ - pkg/inst/testdata/* \ - pkg/R/* - -clean: - @echo "Cleaning up..." - rm -fR pkg.Rcheck - @echo "DONE." - -roxygen: - @echo "Roxygenizing package..." - "$(R_HOME)/bin/Rscript" -e 'library(roxygen2); roxygenize("pkg")' > roxygen.log 2>&1 || cat roxygen.log - @echo "DONE." - -$(TGZ): $(pkgfiles) - @echo "Building package..." - sed -i -e "s/Date:.*/Date: $(DATE)/" pkg/DESCRIPTION - "$(R_HOME)/bin/Rscript" -e 'library(roxygen2); roxygenize("pkg")' > roxygen.log 2>&1 || cat roxygen.log - git log --no-merges -M --date=iso pkg/ > pkg/ChangeLog - "$(R_HOME)/bin/R" CMD build pkg > build.log 2>&1 - @echo "DONE." - -build: $(TGZ) - -check: build - @echo "Running CRAN check..." - "$(R_HOME)/bin/R" CMD check --as-cran $(TGZ) - @echo "DONE." - -install: build - @echo "Installing package..." - "$(R_HOME)/bin/R" CMD INSTALL --no-multiarch $(TGZ) - @echo "DONE." diff --git a/README.rmd b/README.rmd index 77364fb..c87014f 100644 --- a/README.rmd +++ b/README.rmd @@ -33,7 +33,11 @@ library(devtools) install_github("jranke/pfm", subdir = "pkg", quick = TRUE) ``` -To read in and analyse a cwa file: +## Use + +# Analyse TOXSWA output + +Read in and analyse a cwa file: ```{r} library(pfm) @@ -43,3 +47,13 @@ example_cwa$get_events(c(20, 100)) example_cwa$moving_windows(c(7, 21)) print(example_cwa) ``` + +# Calculate PEC soil + +Simple PEC soil calculation for an application rate of 100 g/ha and +25% interception, assuming complete mixing into 5 cm and a soil bulk +density of 1.5 kg/L, output in mg/kg: + +```{r} +PEC_soil(100, int = 0.25) +``` diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index 29e36e7..a327ae1 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -1,11 +1,14 @@ Package: pfm Type: Package Title: Utilities for pesticide fate modelling -Version: 0.1-1 -Date: 2015-01-23 -Authors@R: "Johannes Ranke [aut, cre, cph]" -Description: Utilities for dealing with data from FOCUS software tools. +Version: 0.1-2 +Date: 2015-04-22 +Authors@R: person("Johannes Ranke", email = "jranke@uni-bremen.de", + role = c("aut", "cre", "cph")) +Description: Utilities for simple PEC calculations and for dealing with data + from some FOCUS pesticide fate modelling software. Depends: R6 +Suggests: testthat License: GPL LazyLoad: yes LazyData: yes diff --git a/pkg/NAMESPACE b/pkg/NAMESPACE index 4b55df5..dd140d0 100644 --- a/pkg/NAMESPACE +++ b/pkg/NAMESPACE @@ -1,6 +1,7 @@ -# Generated by roxygen2 (4.1.0): do not edit by hand +# Generated by roxygen2 (4.1.0.9001): do not edit by hand S3method(plot,TOXSWA_cwa) +export(PEC_soil) export(TOXSWA_cwa) export(read.TOXSWA_cwa) importFrom(R6,R6Class) diff --git a/pkg/R/PEC_soil.R b/pkg/R/PEC_soil.R new file mode 100644 index 0000000..3dbc2eb --- /dev/null +++ b/pkg/R/PEC_soil.R @@ -0,0 +1,45 @@ +# Copyright (C) 2015 Johannes Ranke +# Contact: jranke@uni-bremen.de +# This file is part of the R package pfm + +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + +#' Calculate predicted environmental concentrations in soil +#' +#' This is a basic, vectorised form of a simple calculation of a contaminant +#' concentration in bulk soil based on complete, instantaneous mixing. +#' +#' @param rate Application rate in units specified below +#' @param rate_units Defaults to g/ha +#' @param interception The fraction of the application rate that does not reach the soil +#' @param mixing_depth Mixing depth in cm +#' @param bulk_density Bulk density of the soil. Defaults to 1.5 kg/L, or 1500 kg/m3 +#' @param PEC_units Requested units for the calculated PEC. Only mg/kg currently supported +#' @return The predicted concentration in soil +#' @export +#' @author Johannes Ranke +#' @examples +#' PEC_soil(100, interception = 0.25) +PEC_soil <- function(rate, rate_units = "g/ha", interception = 0, + mixing_depth = 5, bulk_density = 1.5, + PEC_units = "mg/kg") +{ + rate_to_soil = (1 - interception) * rate + rate_units = match.arg(rate_units) + PEC_units = match.arg(PEC_units) + soil_volume = 100 * 100 * (mixing_depth/100) # in m3 + soil_mass = soil_volume * bulk_density * 1000 # in kg + PEC_soil = rate_to_soil * 1000 / soil_mass # in mg/kg + return(PEC_soil) +} diff --git a/pkg/man/PEC_soil.Rd b/pkg/man/PEC_soil.Rd new file mode 100644 index 0000000..76ffd4b --- /dev/null +++ b/pkg/man/PEC_soil.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2 (4.1.0.9001): do not edit by hand +% Please edit documentation in R/PEC_soil.R +\name{PEC_soil} +\alias{PEC_soil} +\title{Calculate predicted environmental concentrations in soil} +\usage{ +PEC_soil(rate, rate_units = "g/ha", interception = 0, mixing_depth = 5, + bulk_density = 1.5, PEC_units = "mg/kg") +} +\arguments{ +\item{rate}{Application rate in units specified below} + +\item{rate_units}{Defaults to g/ha} + +\item{interception}{The fraction of the application rate that does not reach the soil} + +\item{mixing_depth}{Mixing depth in cm} + +\item{bulk_density}{Bulk density of the soil. Defaults to 1.5 kg/L, or 1500 kg/m3} + +\item{PEC_units}{Requested units for the calculated PEC. Only mg/kg currently supported} +} +\value{ +The predicted concentration in soil +} +\description{ +This is a basic, vectorised form of a simple calculation of a contaminant +concentration in bulk soil based on complete, instantaneous mixing. +} +\examples{ +PEC_soil(100, interception = 0.25) +} +\author{ +Johannes Ranke +} + diff --git a/pkg/man/TOXSWA_cwa.Rd b/pkg/man/TOXSWA_cwa.Rd index c4d573b..5aa8bfc 100644 --- a/pkg/man/TOXSWA_cwa.Rd +++ b/pkg/man/TOXSWA_cwa.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.0.9001): do not edit by hand % Please edit documentation in R/TOXSWA_cwa.R \docType{class} \name{TOXSWA_cwa} diff --git a/pkg/man/plot.TOXSWA_cwa.Rd b/pkg/man/plot.TOXSWA_cwa.Rd index 9136c10..1dddb78 100644 --- a/pkg/man/plot.TOXSWA_cwa.Rd +++ b/pkg/man/plot.TOXSWA_cwa.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.0.9001): do not edit by hand % Please edit documentation in R/TOXSWA_cwa.R \name{plot.TOXSWA_cwa} \alias{plot.TOXSWA_cwa} diff --git a/pkg/man/read.TOXSWA_cwa.Rd b/pkg/man/read.TOXSWA_cwa.Rd index ded0a39..f67455f 100644 --- a/pkg/man/read.TOXSWA_cwa.Rd +++ b/pkg/man/read.TOXSWA_cwa.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.0.9001): do not edit by hand % Please edit documentation in R/TOXSWA_cwa.R \name{read.TOXSWA_cwa} \alias{read.TOXSWA_cwa} diff --git a/pkg/tests/testthat.R b/pkg/tests/testthat.R new file mode 100644 index 0000000..d2df1cc --- /dev/null +++ b/pkg/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(pfm) + +test_check("pfm") diff --git a/pkg/tests/testthat/H_sw_D4_pond.rds b/pkg/tests/testthat/H_sw_D4_pond.rds new file mode 100644 index 0000000..9ad8194 Binary files /dev/null and b/pkg/tests/testthat/H_sw_D4_pond.rds differ diff --git a/pkg/tests/testthat/H_sw_R1_stream_events.rds b/pkg/tests/testthat/H_sw_R1_stream_events.rds new file mode 100644 index 0000000..3e2828e Binary files /dev/null and b/pkg/tests/testthat/H_sw_R1_stream_events.rds differ diff --git a/pkg/tests/testthat/H_sw_R1_stream_windows.rds b/pkg/tests/testthat/H_sw_R1_stream_windows.rds new file mode 100644 index 0000000..29e13f2 Binary files /dev/null and b/pkg/tests/testthat/H_sw_R1_stream_windows.rds differ diff --git a/pkg/tests/testthat/test_PEC_soil.R b/pkg/tests/testthat/test_PEC_soil.R new file mode 100644 index 0000000..27b2eb7 --- /dev/null +++ b/pkg/tests/testthat/test_PEC_soil.R @@ -0,0 +1,13 @@ +library(pfm) +context("Simple PEC soil calculations") + +test_that("PEC_soil calculates correctly", { + # Application of 100 g/ha gives 0.133 mg/kg under default assumptions + expect_equal(PEC_soil(100), 0.1 * 4/3) + + # or 0.1 mg/kg assuming 25% interception + expect_equal(PEC_soil(100, interception = 0.25), 0.1) + + # Mixing depth of 1 cm gives five-fold PEC + expect_equal(PEC_soil(100, interception = 0.25, mixing_depth = 1), 0.5) +}) diff --git a/pkg/tests/testthat/test_TOXSWA_cwa.R b/pkg/tests/testthat/test_TOXSWA_cwa.R new file mode 100644 index 0000000..d91b79a --- /dev/null +++ b/pkg/tests/testthat/test_TOXSWA_cwa.R @@ -0,0 +1,25 @@ +library(pfm) +context("Read and analyse TOXSWA cwa files") + +test_that("TOXSWA cwa file is correctly read", { + H_sw_D4_pond <- read.TOXSWA_cwa("00001p_pa.cwa", + basedir = "SwashProjects/project_H_sw/TOXSWA", + zipfile = system.file("testdata/SwashProjects.zip", + package = "pfm")) + expect_equal_to_reference(H_sw_D4_pond, file = "H_sw_D4_pond.rds") +}) + +test_that("Getting events and moving window analysis works", { + H_sw_R1_stream <- read.TOXSWA_cwa("00003s_pa.cwa", + basedir = "SwashProjects/project_H_sw/TOXSWA", + zipfile = system.file("testdata/SwashProjects.zip", + package = "pfm")) + + # Event analysis with two different thresholds + H_sw_R1_stream$get_events(c(2, 10)) + expect_equal_to_reference(H_sw_R1_stream$events, file = "H_sw_R1_stream_events.rds") + + # Moving window analysis + H_sw_R1_stream$moving_windows(c(7, 21)) + expect_equal_to_reference(H_sw_R1_stream$windows, file = "H_sw_R1_stream_windows.rds") +}) -- cgit v1.2.1