Skip to content

Commit

Permalink
Rename to profile_ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed Nov 13, 2023
1 parent c5efef0 commit e010c25
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export(aka)
export(as_label)
export(as_name)
export(emissions_profile)
export(emissions_profile_any_add_values_to_categorize)
export(emissions_profile_any_add_profile_ranking)
export(emissions_profile_upstream)
export(enquo)
export(enquos)
Expand Down
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

# tiltIndicator 0.0.0.9098

* `emissions_profile_any_add_values_to_categorize()` now relocates the new
* `emissions_profile_any_add_profile_ranking()` now relocates the new
columns to the left.

# tiltIndicator 0.0.0.9097

* `emissions_profile*()` uses `co2$values_to_categorize` if present (#605).
* `emissions_profile*()` uses `co2$profile_ranking` if present (#605).

# tiltIndicator 0.0.0.9096

* New pre-processing helper `emissions_profile_any_add_values_to_categorize()`
* New pre-processing helper `emissions_profile_any_add_profile_ranking()`
(#602).

# tiltIndicator 0.0.0.9095
Expand Down
18 changes: 9 additions & 9 deletions R/emissions_profile_any_add_values_to_categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @family pre-processing helpers
#'
#' @return The input data frame with the additional columns `grouped_by` and
#' `values_to_categorize` and one row per benchmark per company.
#' `profile_ranking` and one row per benchmark per company.
#'
#' @export
#'
Expand All @@ -18,25 +18,25 @@
#' companies <- read_csv(toy_emissions_profile_any_companies())
#'
#' products <- read_csv(toy_emissions_profile_products())
#' products |> emissions_profile_any_add_values_to_categorize()
#' products |> emissions_profile_any_add_profile_ranking()
#'
#' inputs <- read_csv(toy_emissions_profile_upstream_products())
#' inputs |> emissions_profile_any_add_values_to_categorize()
emissions_profile_any_add_values_to_categorize <- function(data) {
check_emissions_profile_any_add_values_to_categorize(data)
#' inputs |> emissions_profile_any_add_profile_ranking()
emissions_profile_any_add_profile_ranking <- function(data) {
check_emissions_profile_any_add_profile_ranking(data)

if (hasName(data, "values_to_categorize")) {
if (hasName(data, "profile_ranking")) {
out <- check_crucial_names(data, "grouped_by")
} else {
benchmarks <- set_names(epa_benchmarks(data), flat_benchmarks(data))
out <- map_df(benchmarks, ~ add_rank(data, .x), .id = "grouped_by")
}

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

check_emissions_profile_any_add_values_to_categorize <- function(data) {
check_emissions_profile_any_add_profile_ranking <- function(data) {
crucial <- c(aka("tsector"), aka("xunit"), aka("isic"), aka("co2footprint"))
walk(crucial, \(pattern) check_matches_name(data, pattern))
}
Expand Down Expand Up @@ -64,7 +64,7 @@ add_rank <- function(data, .by) {
if (identical(.by, "all")) .by <- NULL
mutate(
data,
values_to_categorize = rank_proportion(.data[[extract_name(data, aka("co2footprint"))]]),
profile_ranking = rank_proportion(.data[[extract_name(data, aka("co2footprint"))]]),
.by = all_of(.by)
)
}
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_add_values_to_categorize() |>
emissions_profile_any_add_profile_ranking() |>
add_risk_category(low_threshold, high_threshold) |>
join_companies(.companies) |>
epa_select_cols_at_product_level() |>
Expand Down
2 changes: 1 addition & 1 deletion R/sector_profile_at_product_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sector_profile_at_product_level <- function(companies,
.scenarios <- prepare_scenarios(scenarios, low_threshold, high_threshold)

.companies |>
spa_add_values_to_categorize(.scenarios) |>
spa_add_profile_ranking(.scenarios) |>
add_risk_category(low_threshold, high_threshold, .default = NA) |>
spa_polish_output_at_product_level() |>
sp_select_cols_at_product_level() |>
Expand Down
2 changes: 1 addition & 1 deletion R/sector_profile_upstream_at_product_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sector_profile_upstream_at_product_level <- function(companies,
.inputs <- prepare_inputs(inputs)

.inputs |>
spa_add_values_to_categorize(.scenarios) |>
spa_add_profile_ranking(.scenarios) |>
add_risk_category(low_threshold, high_threshold, .default = NA) |>
join_companies(remove_col_scenario(.companies)) |>
spa_polish_output_at_product_level() |>
Expand Down
4 changes: 2 additions & 2 deletions R/spa.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ prepare_scenarios <- function(data, low_threshold, high_threshold) {
data |>
mutate(low_threshold = low_threshold, high_threshold = high_threshold) |>
distinct() |>
rename(values_to_categorize = aka("co2reduce"))
rename(profile_ranking = aka("co2reduce"))
}

spa_add_values_to_categorize <- function(data, scenarios) {
spa_add_profile_ranking <- function(data, scenarios) {
left_join(
data, scenarios,
by = c(aka("scenario_type"), aka("xsector"), aka("xsubsector")),
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ abort_missing_names <- function(missing_names) {

add_risk_category <- function(data, low_threshold, high_threshold, ...) {
mutate(data, risk_category = categorize_risk(
.data$values_to_categorize, .data$low_threshold, .data$high_threshold, ...
.data$profile_ranking, .data$low_threshold, .data$high_threshold, ...
))
}

Expand Down
14 changes: 7 additions & 7 deletions man/emissions_profile_any_add_values_to_categorize.Rd

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

2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
test_that("works with any 'co2-like' dataset", {
co2 <- example_products()
expect_no_error(emissions_profile_any_add_values_to_categorize(co2))
expect_no_error(emissions_profile_any_add_profile_ranking(co2))

co2 <- example_inputs()
expect_no_error(emissions_profile_any_add_values_to_categorize(co2))
expect_no_error(emissions_profile_any_add_profile_ranking(co2))
})

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

out <- emissions_profile_any_add_values_to_categorize(co2)
out <- emissions_profile_any_add_profile_ranking(co2)

new_names <- c("grouped_by", "values_to_categorize")
new_names <- c("grouped_by", "profile_ranking")
expect_equal(setdiff(names(out), names(co2)), new_names)
})

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

out <- emissions_profile_any_add_values_to_categorize(co2)
out <- emissions_profile_any_add_profile_ranking(co2)

new_names <- c("grouped_by", "values_to_categorize")
new_names <- c("grouped_by", "profile_ranking")
expect_equal(names(out)[1:2], new_names)
})

test_that("with one company, adds one row per benchmark per company", {
co2 <- example_products()

out <- emissions_profile_any_add_values_to_categorize(co2)
out <- emissions_profile_any_add_profile_ranking(co2)

number_of_benchmarks <- length(flat_benchmarks(co2))
expect_equal(nrow(out), number_of_benchmarks)
Expand All @@ -36,7 +36,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_add_values_to_categorize(co2)
out <- emissions_profile_any_add_profile_ranking(co2)

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

crucial <- aka("tsector")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)
expect_error(emissions_profile_any_add_profile_ranking(bad), crucial)

crucial <- aka("xunit")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)
expect_error(emissions_profile_any_add_profile_ranking(bad), crucial)

crucial <- aka("isic")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)
expect_error(emissions_profile_any_add_profile_ranking(bad), crucial)

crucial <- aka("co2footprint")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)
expect_error(emissions_profile_any_add_profile_ranking(bad), crucial)
})

test_that("if `values_to_categorize` is present, `grouped_by` must be present", {
co2 <- example_products(values_to_categorize = 1)
test_that("if `profile_ranking` is present, `grouped_by` must be present", {
co2 <- example_products(profile_ranking = 1)

crucial <- "grouped_by"
expect_error(emissions_profile_any_add_values_to_categorize(co2), crucial)
expect_error(emissions_profile_any_add_profile_ranking(co2), crucial)
})
28 changes: 14 additions & 14 deletions tests/testthat/test-emissions_profile_any_at_product_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -287,46 +287,46 @@ test_that("`*rowid` columns are passed through inputs with duplicates", {
expect_true(hasName(out, "products_rowid"))
})

test_that("with products, uses `co2$values_to_categorize` if present (#603)", {
test_that("with products, uses `co2$profile_ranking` if present (#603)", {
companies <- example_companies()
co2 <- example_products(!!aka("uid") := c("a", "b"))
co2[extract_name(co2, aka("co2footprint"))] <- 1:2

lacks_values_to_categorize <- !hasName(co2, "values_to_categorize")
stopifnot(lacks_values_to_categorize)
lacks_profile_ranking <- !hasName(co2, "profile_ranking")
stopifnot(lacks_profile_ranking)
out1 <- emissions_profile_any_at_product_level(companies, co2)
using_computed_values <- unique(out1$risk_category)

pre_computed <- emissions_profile_any_add_values_to_categorize(co2)
has_values_to_categorize <- hasName(pre_computed, "values_to_categorize")
stopifnot(has_values_to_categorize)
pre_computed <- emissions_profile_any_add_profile_ranking(co2)
has_profile_ranking <- hasName(pre_computed, "profile_ranking")
stopifnot(has_profile_ranking)

yields_a_different_risk_category <- 999
pre_computed$values_to_categorize <- yields_a_different_risk_category
pre_computed$profile_ranking <- yields_a_different_risk_category
out2 <- emissions_profile_any_at_product_level(companies, pre_computed)
using_pre_computed_values <- unique(out2$risk_category)

expect_false(identical(using_computed_values, using_pre_computed_values))
})

test_that("with inputs, uses `co2$values_to_categorize` if present (#603)", {
test_that("with inputs, uses `co2$profile_ranking` if present (#603)", {
companies <- example_companies()
co2 <- example_inputs(!!aka("uid") := c("a", "b"))
# This is wrong: names(example_inputs(!!aka("co2footprint") := 1:2))
# It yields two columns: `co2_footprint` AND `input_co2_footprint`
co2[extract_name(co2, aka("co2footprint"))] <- 1:2

lacks_values_to_categorize <- !hasName(co2, "values_to_categorize")
stopifnot(lacks_values_to_categorize)
lacks_profile_ranking <- !hasName(co2, "profile_ranking")
stopifnot(lacks_profile_ranking)
out1 <- emissions_profile_any_at_product_level(companies, co2)
using_computed_values <- unique(out1$risk_category)

pre_computed <- emissions_profile_any_add_values_to_categorize(co2)
has_values_to_categorize <- hasName(pre_computed, "values_to_categorize")
stopifnot(has_values_to_categorize)
pre_computed <- emissions_profile_any_add_profile_ranking(co2)
has_profile_ranking <- hasName(pre_computed, "profile_ranking")
stopifnot(has_profile_ranking)

yields_a_different_risk_category <- 999
pre_computed$values_to_categorize <- yields_a_different_risk_category
pre_computed$profile_ranking <- yields_a_different_risk_category
out2 <- emissions_profile_any_at_product_level(companies, pre_computed)
using_pre_computed_values <- unique(out2$risk_category)

Expand Down

0 comments on commit e010c25

Please sign in to comment.