Skip to content

Commit

Permalink
v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
adrientaudiere committed Nov 19, 2024
1 parent d858c49 commit 643b70d
Show file tree
Hide file tree
Showing 32 changed files with 814 additions and 200 deletions.
11 changes: 11 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@
^docs$
^pkgdown$
^\.github$
^README\.Rmd$
^LICENSE\.md$
^doc$
^Meta$
^CODE_OF_CONDUCT\.md$
^CONTRIBUTING\.md$
^paper$
^.vscode$
^citation.cff$
^vignettes/articles$
^cran-comments\.md$
61 changes: 61 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:

name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v4
with:
# Fail if error if not on PR, or if on PR and token is given
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
file: ./cobertura.xml
plugin: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Package: greenAlgoR
Title: Compute ecological footprint in R
Version: 0.0.0.9000
Version: 0.1
Authors@R: person("Adrien", "Taudière", email = "[email protected]",
role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0003-1088-1182"))
Description: This package computes ecological footprint in R (based on [green-algorithms](https://calculator.green-algorithms.org/).
greenAlgoR also made it simple to compute ecological footprint of {[targets](https://github.com/ropensci/targets)} pipelines.
greenAlgoR also made it simple to compute ecological footprint of \{[targets](https://github.com/ropensci/targets)\} pipelines.
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
export(csv_from_url_ga)
export(ga_footprint)
export(ga_targets)
export(round_conditionaly)
export(session_runtime)
import(benchmarkme)
import(ggplot2)
import(targets)
importFrom(utils,read.csv)
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# greenAlgoR 0.1

* Initial CRAN submission.
* Initial github submission.
* Creation of function `ga_footprint()` and `ga_targets()`
83 changes: 57 additions & 26 deletions R/ga_footprint.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#' - TDP_per_core: https://raw.githubusercontent.com/GreenAlgorithms/green-algorithms-tool/refs/heads/master/data/v2.2/TDP_cpu.csv
#' - power_draw_per_gb: https://onlinelibrary.wiley.com/doi/10.1002/advs.202100707
#'
#' Text from the [green-algorithms](https://github.com/GreenAlgorithms/green-algorithms-tool) website:
#' Description of the algorithm from the
#' [green-algorithms](https://github.com/GreenAlgorithms/green-algorithms-tool)
#' website:
#'
#' """
#'
Expand All @@ -41,18 +43,18 @@
#'
#' The Carbon Intensity depends on the location and the technologies used
#' to produce electricity. But note that the "energy needed"
#' [...] is independent of the location.
#' \[...\] is independent of the location.
#'
#' """
#'
#' @param runtime_h Run time in hours (int). If runtime_h == "session",
#' the runtime is compute using the actual R session
#' @param location_code (character list of country or region available in )
#' @param PUE (int) Power usage effectiveness of the server.
#' See https://github.com/GreenAlgorithms/green-algorithms-tool/blob/master/data/v2.2/defaults_PUE.csv
#' for example of values. If you are using your personal computer, set PUE to 1.
#' @param TDP_per_core (int. in Watt, default 12). Find your cpu TDP and your nb of cpu on
#' https://www.techpowerup.com/cpu-specs/ or in
#' @param PUE (int) Power usage effectiveness of the server. See
#' https://github.com/GreenAlgorithms/green-algorithms-tool/blob/master/data/v2.2/defaults_PUE.csv
#' for example of values. If you are using your personal computer, set PUE to 1.
#' @param TDP_per_core (int. in Watt, default 12). Find your cpu TDP and your
#' nb of cpu on https://www.techpowerup.com/cpu-specs/ or in
#' http://calculator.green-algorithms.org/ if available.
#' Owerwrite by cpu_model param.
#' @param n_cores (int, default 1) Number of cores.
Expand Down Expand Up @@ -94,17 +96,20 @@
#' A dataframe with `location` and `carbonIntensity`
#' columns. Set to carbon_intensity_internal if NULL.
#' carbon_intensity_internal is set using command line
#' csv_from_url_ga("https://raw.githubusercontent.com/GreenAlgorithms/green-algorithms-tool/refs/heads/master/data/v2.2/CI_aggregated.csv")
#' csv_from_url_ga("https://raw.githubusercontent.com/GreenAlgorithms/
#' green-algorithms-tool/refs/heads/master/data/v2.2/CI_aggregated.csv")
#' @param TDP_cpu (default NULL). Advanced users only.
#' A dataframe with `model`, `n_cores` and `TDP_per_core`
#' columns. Set to TDP_cpu_internal if NULL.
#' TDP_cpu_internal is set using command line
#' csv_from_url_ga("https://raw.githubusercontent.com/GreenAlgorithms/green-algorithms-tool/refs/heads/master/data/v2.2/TDP_cpu.csv")
#' csv_from_url_ga("https://raw.githubusercontent.com/GreenAlgorithms/
#' green-algorithms-tool/refs/heads/master/data/v2.2/TDP_cpu.csv")
#' @param ref_value (default NULL). Advanced users only.
#' A dataframe with `variable` and `value`
#' columns. Set to ref_value_internal if NULL.
#' ref_value_internal is set using command line
#' csv_from_url_ga("https://raw.githubusercontent.com/GreenAlgorithms/green-algorithms-tool/refs/heads/master/data/v2.2/referenceValues.csv")
#' csv_from_url_ga("https://raw.githubusercontent.com/GreenAlgorithms/
#' green-algorithms-tool/refs/heads/master/data/v2.2/referenceValues.csv")
#'
#' @return A list of values
#' - `runtime_h`: the input run time in hours
Expand Down Expand Up @@ -158,18 +163,44 @@
#'
#' ggplot(res_ga$ref_value, aes(y = variable, x = as.numeric(value), fill = log10(prop_footprint))) +
#' geom_col() +
#' geom_col(data = data.frame(variable = "Total", value = res_ga$carbon_footprint_total_gCO2), fill = "grey30") +
#' geom_col(data = data.frame(variable = "Cores", value = res_ga$carbon_footprint_cores), fill = "darkred") +
#' geom_col(data = data.frame(variable = "Memory", value = res_ga$carbon_footprint_memory), fill = "orange") +
#' geom_col(data = data.frame(variable = "Mass storage", value = res_ga$carbon_footprint_storage), fill = "violet") +
#' scale_x_continuous(trans = "log1p") +
#' geom_vline(xintercept = res_ga$carbon_footprint_total_gCO2, col = "grey30", lwd = 1.2) +
#' geom_label(aes(label = round_conditionaly(prop_footprint)), fill = "grey90",position = position_stack(vjust = 1.1)) +
#' labs(title="Carbon footprint of the analysis",
#' subtitle = paste0("(", res_ga$carbon_footprint_total_gCO2, " g CO2",")"),
#' caption = "Please cite Lannelongue et al. 2021 (10.1002/advs.202100707)") +
#' geom_col(data = data.frame(
#' variable = "Total",
#' value = res_ga$carbon_footprint_total_gCO2
#' ), fill = "grey30") +
#' geom_col(data = data.frame(
#' variable = "Cores",
#' value = res_ga$carbon_footprint_cores
#' ), fill = "darkred") +
#' geom_col(data = data.frame(
#' variable = "Memory",
#' value = res_ga$carbon_footprint_memory
#' ), fill = "orange") +
#' geom_col(data = data.frame(
#' variable = "Mass storage",
#' value = res_ga$carbon_footprint_storage
#' ), fill = "violet") +
#' scale_x_continuous(
#' trans = "log1p",
#' breaks = c(0, 10^c(1:max(log1p(as.numeric(res_ga$ref_value$value)))))
#' ) +
#' geom_vline(
#' xintercept = res_ga$carbon_footprint_total_gCO2,
#' col = "grey30", lwd = 1.2
#' ) +
#' geom_label(aes(label = round_conditionaly(prop_footprint)),
#' fill = "grey90", position = position_stack(vjust = 1.1)
#' ) +
#' labs(
#' title = "Carbon footprint of the analysis",
#' subtitle = paste0(
#' "(", res_ga$carbon_footprint_total_gCO2,
#' " g CO2", ")"
#' ),
#' caption = "Please cite Lannelongue et al. 2021 (10.1002/advs.202100707)"
#' ) +
#' xlab("Carbon footprint (g CO2) in log10") +
#' ylab("Modality")
#' ylab("Modality") +
#' theme(legend.position = "none")
ga_footprint <- function(runtime_h = NULL,
location_code = "WORLD",
PUE = 1.67,
Expand Down Expand Up @@ -269,7 +300,7 @@ ga_footprint <- function(runtime_h = NULL,

if (add_ref_values) {
if (is.null(ref_value)) {
ref_value <- ref_value_internal[order(as.numeric(ref_value_internal$value)),]
ref_value <- ref_value_internal[order(as.numeric(ref_value_internal$value)), ]
rownames(ref_value) <- NULL
}
res[["ref_value"]] <- rbind(
Expand All @@ -291,7 +322,7 @@ ga_footprint <- function(runtime_h = NULL,
res[["energy_needed_kWh"]] <- res[["energy_needed_kWh"]] + res[["power_draw_storage_kWh"]]
res[["carbon_footprint_total_gCO2"]] <- res[["carbon_footprint_cores"]] + res[["carbon_footprint_memory"]] + res[["carbon_footprint_storage"]]

if (add_ref_values) {
if (add_ref_values) {
res[["ref_value"]] <- rbind(
c("Total", res$carbon_footprint_total_gCO2, NA),
c("Cores", res$carbon_footprint_cores, NA),
Expand All @@ -308,9 +339,9 @@ ga_footprint <- function(runtime_h = NULL,

# to force ggplot to keep row order
res[["ref_value"]]$variable <- factor(res[["ref_value"]]$variable,
levels = res[["ref_value"]]$variable )

}
levels = res[["ref_value"]]$variable
)
}

return(res)
}
50 changes: 40 additions & 10 deletions R/ga_targets.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Compute footprint in grams of CO2 for {targets} pipelines
#' Compute footprint in grams of CO2 for \{targets\} pipelines
#'
#' @description
#'
Expand Down Expand Up @@ -40,7 +40,10 @@
#' command = Sys.sleep(2),
#' description = "Sleep 2 seconds"
#' ),
#' tar_target(x, writeLines(targets::tar_option_get("error"), "error.txt"))
#' tar_target(x, writeLines(
#' targets::tar_option_get("error"),
#' "error.txt"
#' ))
#' )
#' },
#' ask = FALSE
Expand All @@ -59,14 +62,41 @@
#' add_storage_estimation = TRUE
#' )
#'
#' ggplot(res_gat$ref_value, aes(y = reorder(variable, as.numeric(value)), x = as.numeric(value), fill = log10(prop_footprint))) +
#' ggplot(res_gat$ref_value, aes(
#' y = reorder(variable, as.numeric(value)),
#' x = as.numeric(value), fill = log10(prop_footprint)
#' )) +
#' geom_col() +
#' geom_col(data = data.frame(variable = "Total ", value = res_gat$carbon_footprint_gCO2), fill = "grey30") +
#' geom_col(data = data.frame(variable = "Cores", value = res_gat$carbon_intensity * res_gat$power_draw_for_cores_kWh), fill = "darkred") +
#' geom_col(data = data.frame(variable = "Memory", value = res_gat$carbon_intensity * res_gat$power_draw_for_memory_kWh), fill = "orange") +
#' geom_col(data = data.frame(variable = "Storage", value = res_gat$carbon_intensity * res_gat$power_draw_per_gb), fill = "violet") +
#' geom_col(data = data.frame(
#' variable = "Total ",
#' value = res_gat$carbon_footprint_total_gCO2
#' ), fill = "grey30") +
#' geom_col(
#' data = data.frame(
#' variable = "Cores",
#' value = res_gat$carbon_intensity * res_gat$power_draw_for_cores_kWh
#' ),
#' fill = "darkred"
#' ) +
#' geom_col(
#' data = data.frame(
#' variable = "Memory",
#' value = res_gat$carbon_intensity * res_gat$power_draw_for_memory_kWh
#' ),
#' fill = "orange"
#' ) +
#' geom_col(
#' data = data.frame(
#' variable = "Storage",
#' value = res_gat$carbon_intensity * res_gat$power_draw_per_gb
#' ),
#' fill = "violet"
#' ) +
#' scale_x_continuous(trans = "log1p") +
#' geom_vline(xintercept = res_gat$carbon_footprint_gCO2, col = "grey30", lwd = 1.2) +
#' geom_vline(
#' xintercept = res_gat$carbon_footprint_total_gCO2,
#' col = "grey30", lwd = 1.2
#' ) +
#' geom_label(aes(label = round(prop_footprint, 1)), fill = "grey90") +
#' xlab("g CO^2") +
#' ylab("Modality")
Expand Down Expand Up @@ -121,8 +151,8 @@ ga_targets <- function(names = NULL,
df_meta <- tar_meta_raw
}

runtime_targets <- sum(df_meta$seconds, na.rm = TRUE) / 3600
power_draw_stocks <- sum(df_meta$bytes, na.rm = TRUE) / 10^9
runtime_targets <- sum(as.numeric(df_meta$seconds), na.rm = TRUE) / 3600
power_draw_stocks <- sum(as.numeric(df_meta$bytes), na.rm = TRUE) / 10^9

res <- ga_footprint(
runtime_h = as.numeric(runtime_targets),
Expand Down
7 changes: 7 additions & 0 deletions R/greenAlgo-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' \code{greenAlgoR} package
#'
#' This package computes ecological footprint in R (based on [green-algorithms](https://calculator.green-algorithms.org/).
#' greenAlgoR also made it simple to compute ecological footprint of {[targets](https://github.com/ropensci/targets)} pipelines..
#' @name greenAlgoR-package
#' @import ggplot2 targets benchmarkme
NULL
Loading

0 comments on commit 643b70d

Please sign in to comment.