Skip to content

Commit cc0a96d

Browse files
committed
refactor
1 parent 51770df commit cc0a96d

File tree

8 files changed

+68
-28
lines changed

8 files changed

+68
-28
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ importFrom(dplyr,inner_join)
7575
importFrom(dplyr,left_join)
7676
importFrom(dplyr,mutate)
7777
importFrom(dplyr,n)
78+
importFrom(dplyr,n_distinct)
7879
importFrom(dplyr,near)
7980
importFrom(dplyr,pull)
8081
importFrom(dplyr,reframe)

R/compute_profile_ranking.R

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ assign_na_to_profile_ranking_in_special_cases <- function(data, excluded_uuids)
8282
data |> should_be_na_when_missing(aka("isic")) ~ NA,
8383
data |> should_be_na_when_missing(aka("tsubsector")) ~ NA,
8484
data |> should_be_na_when_missing(aka("xunit")) ~ NA,
85-
data |> should_be_na_when_unique_uuids_is_1_for_unit_isic(excluded_uuids, "unit_isic_4digit") ~ NA,
85+
data |> should_be_na_when_unique_uuids_is_1_for_unit_isic(
86+
excluded_uuids,
87+
extract_name(data, aka("xunit")),
88+
extract_name(data, aka("isic"))
89+
) ~ NA,
8690
.default = .data$profile_ranking
8791
))
8892
}
@@ -105,13 +109,28 @@ should_be_na_when_missing <- function(data, pattern) {
105109
}
106110

107111
pull_uuids_with_unique_uuid_count_unit_isic_1 <- function(data) {
112+
if ("input_activity_uuid_product_uuid" %in% colnames(data)) {
113+
uuid <- "input_activity_uuid_product_uuid"
114+
} else {
115+
uuid <- "activity_uuid_product_uuid"
116+
}
117+
108118
data |>
109-
mutate(unique_uuid_count_unit_isic = n_distinct(.data$activity_uuid_product_uuid), .by = c("unit", "isic_4digit")) |>
119+
mutate(
120+
unique_uuid_count_unit_isic = n_distinct(.data[[uuid]]),
121+
.by = all_of(c(extract_name(data, aka("xunit")), extract_name(data, aka("isic"))))
122+
) |>
110123
filter(.data$unique_uuid_count_unit_isic == 1) |>
111-
pull(activity_uuid_product_uuid) |>
124+
pull(.data[[uuid]]) |>
112125
unique()
113126
}
114127

115-
should_be_na_when_unique_uuids_is_1_for_unit_isic <- function(data, excluded_uuids, pattern) {
116-
(get_column(data, aka("uid")) %in% excluded_uuids) & is_benchmark_to_exclude(data, pattern)
128+
should_be_na_when_unique_uuids_is_1_for_unit_isic <- function(data, excluded_uuids, pattern_unit, pattern_isic) {
129+
if ("input_activity_uuid_product_uuid" %in% colnames(data)) {
130+
uuid <- "input_activity_uuid_product_uuid"
131+
} else {
132+
uuid <- "activity_uuid_product_uuid"
133+
}
134+
135+
(get_column(data, uuid) %in% excluded_uuids) & is_benchmark_to_exclude(data, paste0(pattern_unit, "_", pattern_isic))
117136
}

R/tiltIndicator-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#' @importFrom dplyr left_join
2222
#' @importFrom dplyr mutate
2323
#' @importFrom dplyr n
24+
#' @importFrom dplyr n_distinct
2425
#' @importFrom dplyr near
2526
#' @importFrom dplyr pull
2627
#' @importFrom dplyr reframe

tests/testthat/_snaps/emissions_profile.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2 high
2828
3 high
2929
4 high
30-
5 high
30+
5 <NA>
3131
6 high
3232
3333
[[4]]
@@ -36,7 +36,7 @@
3636
2 1
3737
3 1
3838
4 1
39-
5 1
39+
5 NA
4040
6 1
4141
4242
[[5]]
@@ -171,10 +171,10 @@
171171
14 0
172172
15 0
173173
16 0
174-
17 1
174+
17 0
175175
18 0
176176
19 0
177-
20 0
177+
20 1
178178
21 1
179179
22 0
180180
23 0

tests/testthat/_snaps/emissions_profile_upstream.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2 high
2828
3 high
2929
4 high
30-
5 high
30+
5 <NA>
3131
6 high
3232
3333
[[4]]
@@ -36,7 +36,7 @@
3636
2 1.0000
3737
3 1.0000
3838
4 1.0000
39-
5 1.0000
39+
5 NA
4040
6 1.0000
4141
4242
[[5]]
@@ -180,10 +180,10 @@
180180
14 0
181181
15 0
182182
16 0
183-
17 1
183+
17 0
184184
18 0
185185
19 0
186-
20 0
186+
20 1
187187
21 1
188188
22 0
189189
23 0
@@ -202,10 +202,10 @@
202202
2 a input_isic_4digit medium 0.2
203203
3 a input_isic_4digit low 0.2
204204
4 a input_isic_4digit <NA> 0.4
205-
5 a input_unit_input_isic_4digit high 0.2
206-
6 a input_unit_input_isic_4digit medium 0.2
207-
7 a input_unit_input_isic_4digit low 0.2
208-
8 a input_unit_input_isic_4digit <NA> 0.4
205+
5 a input_unit_input_isic_4digit high 0
206+
6 a input_unit_input_isic_4digit medium 0
207+
7 a input_unit_input_isic_4digit low 0
208+
8 a input_unit_input_isic_4digit <NA> 1
209209

210210
---
211211

tests/testthat/test-emissions_profile.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ test_that("in each benchmark, `profile_ranking` increases with `*co2_footprint`"
3535
companies <- example_companies()
3636
co2 <- example_products(co2_footprint = -1:1)
3737

38-
out <- unnest_product(emissions_profile(companies, co2))
38+
out <- unnest_product(emissions_profile(companies, co2)) |>
39+
filter(grouped_by != "unit_isic_4digit")
3940

4041
in_all_benchmarks_profile_ranking_increases_with_co2_footprint <- out |>
4142
group_by(grouped_by) |>
@@ -136,7 +137,8 @@ test_that("at company level, with two matched products and `NA` in one benchmark
136137
)
137138

138139
out <- emissions_profile(companies, co2) |>
139-
unnest_company()
140+
unnest_company() |>
141+
filter(grouped_by != "unit_isic_4digit")
140142
# expect `0.5` where `risk_category` is `NA`
141143
out |>
142144
filter(grepl(benchmark, grouped_by)) |>
@@ -340,6 +342,7 @@ test_that("at company level, 1 matched product yields `value = 1` in 1 `risk_cat
340342

341343
out <- emissions_profile(companies, co2) |>
342344
unnest_company() |>
345+
filter(grouped_by != "unit_isic_4digit") |>
343346
distinct(risk_category, value) |>
344347
pull(value)
345348

@@ -370,6 +373,7 @@ test_that("at company level, one matched and one unmatched products yield `value
370373

371374
out <- emissions_profile(companies, co2) |>
372375
unnest_company() |>
376+
filter(grouped_by != "unit_isic_4digit") |>
373377
distinct(risk_category, value)
374378

375379
na <- pull(filter(out, is.na(risk_category)), value)
@@ -459,6 +463,7 @@ test_that("at company level, 1 matched product, one missing benchmark, and one u
459463

460464
isic <- emissions_profile(companies, co2) |>
461465
unnest_company() |>
466+
filter(grouped_by != "unit_isic_4digit") |>
462467
filter(grepl(aka("isic"), grouped_by)) |>
463468
distinct(risk_category, value)
464469

tests/testthat/test-emissions_profile_upstream.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ test_that("for a company with 3 products of varying footprints, value is 1/3 (#2
8282
)
8383

8484
product <- emissions_profile_any_at_product_level(companies, inputs, low_threshold, high_threshold)
85-
out <- epa_at_company_level(product)
85+
out <- epa_at_company_level(product) |>
86+
filter(grouped_by != "input_unit_input_isic_4digit")
8687
expect_true(identical(unique(out$value), expected_value))
8788
})
8889

@@ -136,7 +137,8 @@ test_that("in each benchmark, `profile_ranking` increases with `*co2_footprint`"
136137
companies <- example_companies()
137138
co2 <- example_inputs(input_co2_footprint = -1:1)
138139

139-
out <- unnest_product(emissions_profile_upstream(companies, co2))
140+
out <- unnest_product(emissions_profile_upstream(companies, co2)) |>
141+
filter(grouped_by != "input_unit_input_isic_4digit")
140142

141143
in_all_benchmarks_profile_ranking_increases_with_co2_footprint <- out |>
142144
group_by(grouped_by) |>
@@ -222,7 +224,8 @@ test_that("at company level, with two matched products and `NA` in one benchmark
222224
)
223225

224226
out <- emissions_profile_upstream(companies, co2) |>
225-
unnest_company()
227+
unnest_company() |>
228+
filter(grouped_by != "input_unit_input_isic_4digit")
226229
# expect `0.5` where `risk_category` is `NA`
227230
out |>
228231
filter(grepl(benchmark, grouped_by)) |>
@@ -254,7 +257,8 @@ test_that("at company level, `NA` in the benchmark of 1/3 products yields a `val
254257
)
255258

256259
out <- emissions_profile_upstream(companies, co2) |>
257-
unnest_company()
260+
unnest_company() |>
261+
filter(grouped_by != "input_unit_input_isic_4digit")
258262
# expect `1/3` where `risk_category` is `NA`
259263
out |>
260264
filter(grepl(benchmark, grouped_by)) |>
@@ -427,6 +431,7 @@ test_that("at company level, 1 matched product yields `value = 1` in 1 `risk_cat
427431

428432
out <- emissions_profile_upstream(companies, co2) |>
429433
unnest_company() |>
434+
filter(grouped_by != "input_unit_input_isic_4digit") |>
430435
distinct(risk_category, value) |>
431436
pull(value)
432437

@@ -443,6 +448,7 @@ test_that("at company level, 2 matched products yield `value = 1` in 1 `risk_cat
443448

444449
out <- emissions_profile_upstream(companies, co2) |>
445450
unnest_company() |>
451+
filter(grouped_by != "input_unit_input_isic_4digit") |>
446452
distinct(risk_category, value) |>
447453
pull(value)
448454

@@ -457,6 +463,7 @@ test_that("at company level, one matched and one unmatched products yield `value
457463

458464
out <- emissions_profile_upstream(companies, co2) |>
459465
unnest_company() |>
466+
filter(grouped_by != "input_unit_input_isic_4digit") |>
460467
distinct(risk_category, value)
461468

462469
na <- pull(filter(out, is.na(risk_category)), value)
@@ -473,6 +480,7 @@ test_that("at company level, two matched and one unmatched products yield `value
473480

474481
out <- emissions_profile_upstream(companies, co2) |>
475482
unnest_company() |>
483+
filter(grouped_by != "input_unit_input_isic_4digit") |>
476484
distinct(risk_category, value)
477485

478486
na <- pull(filter(out, is.na(risk_category)), value)
@@ -491,7 +499,8 @@ test_that("at company level, three products with different `co2_footprint` yield
491499
!!aka("ico2footprint") := 1:3,
492500
)
493501

494-
out <- emissions_profile_upstream(companies, co2) |> unnest_company()
502+
out <- emissions_profile_upstream(companies, co2) |> unnest_company()|>
503+
filter(grouped_by != "input_unit_input_isic_4digit")
495504

496505
actual <- out |>
497506
filter(value != 0) |>
@@ -546,6 +555,7 @@ test_that("at company level, 1 matched product, one missing benchmark, and one u
546555

547556
isic <- emissions_profile_upstream(companies, co2) |>
548557
unnest_company() |>
558+
filter(grouped_by != "input_unit_input_isic_4digit") |>
549559
filter(grepl(aka("iisic"), grouped_by)) |>
550560
distinct(risk_category, value)
551561

tests/testthat/test-epa_compute_profile_ranking.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ test_that("`profile_ranking` is `1` for all maximum `*co2_footprint`", {
5858
co2 <- example_products(!!pattern := c(1, 2, 3, 3, 3))
5959

6060
out <- epa_compute_profile_ranking(co2)
61-
max <- filter(out, .data[[pattern]] == max(.data[[pattern]]))
61+
max <- filter(out, .data[[pattern]] == max(.data[[pattern]])) |>
62+
filter(!is.na(profile_ranking))
6263
expect_true(all(max$profile_ranking == 1.0))
6364

64-
other <- filter(out, .data[[pattern]] != max(.data[[pattern]]))
65+
other <- filter(out, .data[[pattern]] != max(.data[[pattern]])) |>
66+
filter(!is.na(profile_ranking))
6567
expect_false(any(other$profile_ranking == 1.0))
6668
})
6769

@@ -70,10 +72,12 @@ test_that("with inputs, `profile_ranking` is `1` for all maximum `*co2_footprint
7072
co2 <- example_inputs(!!pattern := c(1, 2, 3, 3, 3))
7173

7274
out <- epa_compute_profile_ranking(co2)
73-
max <- filter(out, .data[[pattern]] == max(.data[[pattern]]))
75+
max <- filter(out, .data[[pattern]] == max(.data[[pattern]])) |>
76+
filter(!is.na(profile_ranking))
7477
expect_true(all(max$profile_ranking == 1.0))
7578

76-
other <- filter(out, .data[[pattern]] != max(.data[[pattern]]))
79+
other <- filter(out, .data[[pattern]] != max(.data[[pattern]])) |>
80+
filter(!is.na(profile_ranking))
7781
expect_false(any(other$profile_ranking == 1.0))
7882
})
7983

0 commit comments

Comments
 (0)