Skip to content

Commit

Permalink
* emissions_profile_any_compute_profile_ranking() is now internal (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore authored Jan 13, 2024
1 parent ec777a1 commit e4ce5e1
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 35 deletions.
2 changes: 1 addition & 1 deletion R/emissions_profile_any_at_product_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ emissions_profile_any_at_product_level <- function(companies,
.co2 <- prepare_co2(co2, low_threshold, high_threshold)

.co2 |>
emissions_profile_any_compute_profile_ranking() |>
epa_compute_profile_ranking() |>
add_risk_category(low_threshold, high_threshold) |>
join_companies(.companies) |>
epa_select_cols_at_product_level() |>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#' Add values to categorize
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' This function was deprecated because it's internal. Users don't need to
#' interact with the function itself.
#'
#' @param data A "co2-like" data frame -- i.e. containing products or
#' upstream-products (a.k.a. inputs).
#'
#' @family pre-processing helpers
#' @keywords internal
#'
#' @return The input data frame with the additional columns `grouped_by` and
#' `profile_ranking` and one row per benchmark per company.
Expand All @@ -23,27 +29,36 @@
#' inputs <- read_csv(toy_emissions_profile_upstream_products_ecoinvent())
#' inputs |> emissions_profile_any_compute_profile_ranking()
emissions_profile_any_compute_profile_ranking <- function(data) {
check_emissions_profile_any_compute_profile_ranking(data)
lifecycle::deprecate_warn(
"0.0.0.9109",
"emissions_profile_any_compute_profile_ranking()",
details = "This function is now internal."
)
epa_compute_profile_ranking(data)
}

epa_compute_profile_ranking <- function(data) {
check_epa_compute_profile_ranking(data)

exclude <- short_isic(data) |
is.na(get_column(data, aka("isic"))) |
is.na(get_column(data, aka("tsector")))

list(!exclude, exclude) |>
map(\(x) filter(data, x)) |>
map_df(\(data) emissions_profile_any_compute_profile_ranking_impl(data)) |>
map_df(\(data) epa_compute_profile_ranking_impl(data)) |>
assign_na_to_profile_ranking_in_special_cases()
}

emissions_profile_any_compute_profile_ranking_impl <- function(data) {
epa_compute_profile_ranking_impl <- function(data) {
benchmarks <- set_names(epa_benchmarks(data), flat_benchmarks(data))
out <- map_df(benchmarks, \(x) add_rank(data, x), .id = "grouped_by")

related_cols <- c("grouped_by", "profile_ranking")
relocate(out, all_of(related_cols))
}

check_emissions_profile_any_compute_profile_ranking <- function(data) {
check_epa_compute_profile_ranking <- function(data) {
crucial <- c(aka("tsector"), aka("xunit"), aka("isic"), aka("co2footprint"))
walk(crucial, \(pattern) check_matches_name(data, pattern))
}
Expand Down
15 changes: 6 additions & 9 deletions man/emissions_profile_any_compute_profile_ranking.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/sector_profile_any_pivot_type_sector_subsector.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/sector_profile_any_prepare_scenario.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/sector_profile_any_prune_companies.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# emissions_profile_any_compute_profile_ranking() is deprecated

Code
expect_equal(epa_compute_profile_ranking(co2),
emissions_profile_any_compute_profile_ranking(co2))
Condition
Warning:
`emissions_profile_any_compute_profile_ranking()` was deprecated in tiltIndicator 0.0.0.9109.
i This function is now internal.

10 changes: 10 additions & 0 deletions tests/testthat/test-deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ test_that("xstr_polish_output_at_company_level() throws a deprecation warning",
)
expect_warning(xstr_polish_output_at_company_level(result), "deprecated")
})

test_that("emissions_profile_any_compute_profile_ranking() is deprecated", {
co2 <- example_products()
expect_snapshot(
expect_equal(
epa_compute_profile_ranking(co2),
emissions_profile_any_compute_profile_ranking(co2)
)
)
})
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
test_that("works with any 'co2-like' dataset", {
co2 <- example_products()
expect_no_error(emissions_profile_any_compute_profile_ranking(co2))
expect_no_error(epa_compute_profile_ranking(co2))

co2 <- example_inputs()
expect_no_error(emissions_profile_any_compute_profile_ranking(co2))
expect_no_error(epa_compute_profile_ranking(co2))
})

test_that("adds columns `grouped_by` and `profile_ranking` to the left", {
co2 <- example_products()

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

new_names <- c("grouped_by", "profile_ranking")
expect_equal(names(out)[1:2], new_names)
Expand All @@ -18,7 +18,7 @@ test_that("adds columns `grouped_by` and `profile_ranking` to the left", {
test_that("with one company, adds one row per benchmark per company", {
co2 <- example_products()

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

number_of_benchmarks <- length(flat_benchmarks(co2))
expect_equal(nrow(out), number_of_benchmarks)
Expand All @@ -27,7 +27,7 @@ test_that("with one company, adds one row per benchmark per company", {
test_that("with two companies, adds one row per benchmark per company", {
co2 <- example_products(!!aka("id") := c("a", "b"))

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

number_of_benchmarks <- length(flat_benchmarks(co2))
expect_equal(nrow(out), 2 * number_of_benchmarks)
Expand All @@ -38,26 +38,26 @@ test_that("without crucial columns errors gracefully", {

crucial <- aka("tsector")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_compute_profile_ranking(bad), crucial)
expect_error(epa_compute_profile_ranking(bad), crucial)

crucial <- aka("xunit")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_compute_profile_ranking(bad), crucial)
expect_error(epa_compute_profile_ranking(bad), crucial)

crucial <- aka("isic")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_compute_profile_ranking(bad), crucial)
expect_error(epa_compute_profile_ranking(bad), crucial)

crucial <- aka("co2footprint")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_compute_profile_ranking(bad), crucial)
expect_error(epa_compute_profile_ranking(bad), crucial)
})

test_that("`profile_ranking` is `1` for all maximum `*co2_footprint`", {
pattern <- aka("co2footprint")
co2 <- example_products(!!pattern := c(1, 2, 3, 3, 3))

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)
max <- filter(out, .data[[pattern]] == max(.data[[pattern]]))
expect_true(all(max$profile_ranking == 1.0))

Expand All @@ -69,7 +69,7 @@ test_that("with inputs, `profile_ranking` is `1` for all maximum `*co2_footprint
pattern <- paste0("input_", aka("co2footprint"))
co2 <- example_inputs(!!pattern := c(1, 2, 3, 3, 3))

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)
max <- filter(out, .data[[pattern]] == max(.data[[pattern]]))
expect_true(all(max$profile_ranking == 1.0))

Expand All @@ -83,7 +83,7 @@ test_that("`profile_ranking` excludes-rows and is `NA` where `tilt_sector` is `N
co2 <- example_products(!!pattern := c("'1234'", "'1234'", exclude))
co2[find_co2_footprint(co2)] <- c(3, 2, 1)

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

ranking <- unique(out$profile_ranking)
expected <- c(rank_proportion(c(3, 2)), NA)
Expand All @@ -101,7 +101,7 @@ test_that("with inputs, `profile_ranking` excludes-rows and is `NA` where `tilt_
co2 <- example_inputs(!!pattern := c("'1234'", "'1234'", exclude))
co2[find_co2_footprint(co2)] <- c(3, 2, 1)

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

ranking <- unique(out$profile_ranking)
expected <- c(rank_proportion(c(3, 2)), NA)
Expand All @@ -119,7 +119,7 @@ test_that("`profile_ranking` excludes-rows and is `NA` where `*isic_4digit` is `
co2 <- example_products(!!pattern := c("'1234'", "'1234'", exclude))
co2[find_co2_footprint(co2)] <- c(3, 2, 1)

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

ranking <- unique(out$profile_ranking)
expected <- c(rank_proportion(c(3, 2)), NA)
Expand All @@ -137,7 +137,7 @@ test_that("with inputs, `profile_ranking` excludes-rows and is `NA` where `*isic
co2 <- example_inputs(!!pattern := c("'1234'", "'1234'", exclude))
co2[find_co2_footprint(co2)] <- c(3, 2, 1)

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

ranking <- unique(out$profile_ranking)
expected <- c(rank_proportion(c(3, 2)), NA)
Expand All @@ -155,7 +155,7 @@ test_that("`profile_ranking` excludes-rows and is `NA` where `*isic_4digit` has
co2 <- example_products(!!pattern := c("'1234'", "'1234'", exclude))
co2[find_co2_footprint(co2)] <- c(3, 2, 1)

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

ranking <- unique(out$profile_ranking)
expected <- c(rank_proportion(c(3, 2)), NA)
Expand All @@ -173,7 +173,7 @@ test_that("with inputs, `profile_ranking` excludes-rows and is `NA` where `*isic
co2 <- example_inputs(!!pattern := c("'1234'", "'1234'", exclude))
co2[find_co2_footprint(co2)] <- c(3, 2, 1)

out <- emissions_profile_any_compute_profile_ranking(co2)
out <- epa_compute_profile_ranking(co2)

ranking <- unique(out$profile_ranking)
expected <- c(rank_proportion(c(3, 2)), NA)
Expand Down

0 comments on commit e4ce5e1

Please sign in to comment.