Skip to content

Commit

Permalink
Remove support for custom equations (#29). It adds complexity, is bug…
Browse files Browse the repository at this point in the history
…gy and is beyond the scope of a minimum viable product.
  • Loading branch information
maurolepore committed Mar 25, 2019
1 parent 55f9332 commit 086a9f8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 321 deletions.
50 changes: 4 additions & 46 deletions R/allo_find.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
allo_find_impl <- function(data, custom_eqn) {
eqn <- custom_eqn %||%
suppressMessages(fgeo.biomass::default_eqn(allodb::master_tidy()))
abort_if_not_eqn(eqn)
allo_find_impl <- function(data) {
eqn <- suppressMessages(fgeo.biomass::default_eqn(allodb::master_tidy()))

inform("* Matching equations by site and species.")
.by <- c("sp", "site")
Expand All @@ -28,7 +26,6 @@ allo_find_memoised <- memoise::memoise(allo_find_impl)
#' Find allometric equations in allodb or in a custom equations-table.
#'
#' @param data A dataframe as those created with [add_species()].
#' @param custom_eqn A dataframe of class "eqn".
#'
#' @family functions to manipulate equations
#'
Expand All @@ -46,50 +43,11 @@ allo_find_memoised <- memoise::memoise(allo_find_impl)
#'
#' allo_find(census_species)
#'
#' # PROVIDE CUSTOM EQUAITONS ----------------------------------------------
#' # Checks that the structure of your data isn't terriby wrong
#' # BAD
#' try(as_eqn("really bad data"))
#' try(as_eqn(data.frame(1)))
#'
#' # GOOD
#' your_equations <- tibble::tibble(
#' equation_id = c("000001"),
#' site = c("scbi"),
#' sp = c("tilia americana"),
#' # Watning: Fake!
#' eqn = c("exp(-2.48 + 2.4835 * log(dbh))"),
#' eqn_type = c("mixed_hardwood"),
#' anatomic_relevance = c("total aboveground biomass"),
#' dbh_unit = "cm",
#' bms_unit = "g",
#' dbh_min_mm = 0,
#' dbh_max_mm = Inf
#' )
#'
#' class(as_eqn(your_equations))
#'
#' allo_find(census_species, custom_eqn = as_eqn(your_equations))
#'
#' census_species %>%
#' allo_find(custom_eqn = as_eqn(your_equations))
#' @family constructors
allo_find <- function(data, custom_eqn = NULL) {
allo_find <- function(data) {
inform("Assuming `dbh` in [mm] (required to find dbh-specific equations).")
warn_odd_dbh(data$dbh)
allo_find_memoised(data, custom_eqn = custom_eqn)
}

abort_if_not_eqn <- function(custom_eqn) {
if (!inherits(custom_eqn, "eqn")) {
abort(
glue::glue(
"`custom_eqn` must be of class 'eqn'. Did you forget to use `as_eqn()`?"
)
)
}

invisible(custom_eqn)
allo_find_memoised(data)
}

warn_if_species_missmatch <- function(data, eqn) {
Expand Down
1 change: 1 addition & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
prefer_false <- function(x) {
stopifnot(is.logical(x))

if (all(x[!is.na(x)])) {
x
} else {
Expand Down
41 changes: 0 additions & 41 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -118,47 +118,6 @@ with_biomass %>%
arrange(desc(total_biomass))
```

### Providing custom equations

If we have our own equations, we can create an `equations`-like dataset and use it. `as_eqn()` helps us to create such a dataset: It ensures that our data has the correct structure.

```{r, error=TRUE}
# Checks that the structure of your data isn't terriby wrong
# BAD
as_eqn("really bad data")
as_eqn(data.frame(1))
# GOOD
custom_equations <- tibble::tibble(
equation_id = c("000001"),
site = c("scbi"),
sp = c("paulownia tomentosa"),
eqn = c("exp(-2.48 + 2.4835 * log(dbh))"),
eqn_type = c("mixed_hardwood"),
anatomic_relevance = c("total aboveground biomass"),
dbh_unit = "cm",
bms_unit = "g",
dbh_min_mm = 0,
dbh_max_mm = Inf,
)
class(as_eqn(custom_equations))
```

We can now use the argument `custom_eqn` to pass our custom equations to `allo_find()`.

```{r}
allo_find(census_species, custom_eqn = as_eqn(custom_equations))
```

This is what the entire workflow looks like:

```{r}
census_species %>%
allo_find(custom_eqn = as_eqn(custom_equations)) %>%
allo_evaluate()
```

### Issues

Our progress is recorded in this [Kanban project board](https://github.com/forestgeo/allodb/projects/4). Because we still don't support some features, the the biomass result currently is invalid and excludes some trees.
Expand Down
Loading

0 comments on commit 086a9f8

Please sign in to comment.