-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae2f4ca
commit 58736c9
Showing
22 changed files
with
981 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
inst/doc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(Diagnostic_IDGF) | ||
export(IDGF) | ||
export(computeIDGF) | ||
export(exportIDGF) | ||
export(importIDGF) | ||
export(radarIDGF) | ||
importFrom(magrittr,"%>%") | ||
importFrom(stats,na.omit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#' Calcul de l'IDGF | ||
#' | ||
#' @param IDGFdata Données importées par `importIDGF()` | ||
#' | ||
#' @return une liste | ||
#' @importFrom magrittr %>% | ||
#' @export | ||
#' | ||
#' @examples | ||
computeIDGF <- function(IDGFdata){ | ||
|
||
df_standard <- IDGFdata$dataset | ||
|
||
|
||
metrique_calc <- df_standard %>% | ||
dplyr::inner_join(table_metrics, by = "cd_taxon") %>% | ||
dplyr::mutate_at(.vars = dplyr::vars(indiciel:NH4),.funs = dplyr::funs(.*abondance)) %>% | ||
dplyr::group_by(sample) %>% | ||
dplyr::summarise_at(dplyr::vars(indiciel:NH4),sum) %>% | ||
dplyr::mutate_at(.vars = dplyr::vars(MINER:NH4),.funs=dplyr::funs(1-./indiciel)) %>% | ||
dplyr::ungroup() %>% | ||
dplyr::rowwise() %>% | ||
dplyr:: mutate(MORGA = min(COT,DBO5,DCO), | ||
PTROPHIE = min(PO4,PTOT), | ||
NORG = min(NK,NH4)) %>% | ||
dplyr::ungroup() %>% | ||
dplyr::select(-COT:-DCO,-NK,-NH4,-PO4,-PTOT) %>% | ||
tidyr::gather(key = param, value = value, -sample:-indiciel,-halin) %>% | ||
dplyr::inner_join(IDGFdata$metadata, by = "sample") | ||
|
||
|
||
tab_indiciel <- metrique_calc %>% | ||
dplyr::select(sample,indiciel,halin) %>% | ||
unique() %>% | ||
dplyr::mutate(`fiabilité` = dplyr::case_when(indiciel < 300 ~ "Indice non valide, augmenter la pression de comptage", | ||
indiciel >= 300 & indiciel < 360 ~ "Fiabilité : réduite", | ||
indiciel >= 360 ~ "Fiabilité : satisfaisante")) | ||
|
||
|
||
metriques <- metrique_calc %>% | ||
dplyr::group_by(sample,her,param) %>% | ||
dplyr::mutate(EQR = purrr::pmap_dbl(.l = list(value,her,param), .f = Metric2EQR)) %>% # Renvoie metrique/ref | ||
dplyr::ungroup() %>% | ||
dplyr::select(sample,param,EQR) %>% | ||
tidyr::spread(key = param, value = EQR) %>% | ||
dplyr::mutate_at(dplyr::vars(MES:SAT),round,2) | ||
|
||
|
||
note_finale <- metrique_calc %>% | ||
dplyr::group_by(sample,her,param) %>% | ||
dplyr::mutate(EQR = purrr::pmap_dbl(.l = list(value,her,param), .f = Metric2EQR)) %>% # Renvoie metrique/ref | ||
dplyr::ungroup() %>% | ||
dplyr::group_by(sample) %>% | ||
dplyr::summarise(IDGF = mean(EQR)) %>% # Note IDGF = moyenne des EQR | ||
dplyr::ungroup() %>% | ||
dplyr::mutate(IDGF = model_IDGF(IDGF)) %>% | ||
dplyr::mutate(IDGF = round(IDGF,2)) %>% | ||
dplyr::mutate(class = Ratio2Class(IDGF,boundaries = c(0.25,0.50,0.75,0.88),number = FALSE,language = "FR"), | ||
numclass = Ratio2Class(IDGF,boundaries = c(0.25,0.50,0.75,0.88),number = TRUE,language = "FR")) %>% | ||
dplyr::inner_join(metriques, by = "sample") %>% | ||
dplyr::inner_join(tab_indiciel, by = "sample") %>% | ||
dplyr::mutate(indiciel = ifelse(indiciel > 400, yes = 400, no = indiciel)) %>% | ||
dplyr::mutate(halin = round(halin / 400 * 100,2), | ||
indiciel = round(indiciel / 400 * 100,2)) %>% | ||
|
||
dplyr::left_join(IDGFdata$taxons_missing, by = "sample") %>% | ||
dplyr::left_join(IDGFdata$taxons_halins, by = "sample") %>% | ||
dplyr::mutate_at(dplyr::vars(IDGF,class:SAT,Taxons_inconnus,Taxons_halins), | ||
dplyr::funs(ifelse(is.na(.), "", .))) %>% | ||
dplyr::select(id_releve = sample,pourcentage_indiciels = indiciel,pourcentage_halins = halin, MES:SAT,IDGF,NumClasse = numclass, Classe = class, `fiabilité`, Taxons_halins,Taxons_inconnus) %>% | ||
dplyr::rename("MINE."="MINER","SAT.O2"="SAT","Mat.Orga"="MORGA","P-Trophie"="PTROPHIE","N-Orga"="NORG") | ||
|
||
|
||
return(note_finale) | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#' Export des résultats et graphiques issus de l'IDGF | ||
#' | ||
#' @param IDGFres résultats IDGF issus de `computeIDGF()` ou `radarIDGF()` | ||
#' @param outdir Dossier de sortie à définir pour les résultats | ||
#' | ||
#' @return | ||
#' @importFrom magrittr %>% | ||
#' @export | ||
#' | ||
#' @examples | ||
exportIDGF <- function(IDGFres, outdir = paste0("RES_",Sys.Date())){ | ||
|
||
|
||
if(!dir.exists(outdir)) {dir.create(outdir, recursive = TRUE)} | ||
|
||
s.time <- paste0(strsplit(as.character(Sys.time())," ")[[1]],collapse = "_") | ||
time <- paste0(strsplit(s.time,":")[[1]],collapse = "-") | ||
|
||
write.csv2(IDGFres %>% dplyr::select(1:16),paste0(outdir,"/resultats_idgf_",time,".csv"), row.names = FALSE) | ||
|
||
## Sortie des graphiques si ils sont présent dans le tableau de résultat | ||
if(ncol(IDGFres) == 17) { | ||
|
||
output <- file.path(outdir,"Graphiques_diagnostic") | ||
|
||
if(!dir.exists(output)) {dir.create(output, recursive = TRUE)} | ||
|
||
tab_res <- IDGFres %>% dplyr::select(id_releve,plot) %>% | ||
dplyr::mutate(path = file.path(output,id_releve)) %>% | ||
dplyr::mutate(png_path = glue::glue("{path}.png")) | ||
|
||
|
||
export_plot <- function(id_releve,plot,png_path) { | ||
|
||
print(plot) %>% | ||
ggplot2::ggsave(filename=png_path,dpi = "retina", width = 7.62, height = 6.48) | ||
} | ||
|
||
purrr::pwalk(.l = list(tab_res$id_releve,tab_res$plot,tab_res$png_path), .f = export_plot) | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#' Import des données | ||
#' | ||
#' @param input Le nom du fichier de données à importer (ou un chemin) | ||
#' | ||
#' @return une liste | ||
#' @importFrom magrittr %>% | ||
#' @export | ||
#' | ||
#' @examples | ||
importIDGF <- function(input){ | ||
|
||
filetype <- tools::file_ext(input) | ||
|
||
if(filetype %in% c("xls","xlsx")) { | ||
|
||
df <- suppressWarnings(readxl::read_excel(input)) | ||
|
||
} else if (filetype == "csv") { | ||
|
||
df <- suppressWarnings(readr::read_csv2(input) %>% dplyr::select(-X1)) | ||
|
||
} else {stop("Merci d'utiliser un fichier d'entrée au format .xls, .xlsx ou .csv")} | ||
|
||
|
||
# Test unitaires | ||
|
||
if(ncol(df) != 4) { | ||
stop('l\'argument df a besoin d\'un tableau avec 4 colonnes précisément : id_releve, cd_taxon, abondance, her (1 ou 2)') | ||
} | ||
|
||
# Standardisation (transcodage + abondance) | ||
|
||
names(df) <- c("sample","cd_taxon","abondance","her") | ||
if(is.factor(df$cd_taxon)) {df$cd_taxon = as.character(df$cd_taxon)} | ||
|
||
tab_her <- df %>% dplyr::select(sample,her) %>% dplyr::distinct() | ||
|
||
taxons_missing<- subset(df, !(cd_taxon %in% table_metrics$cd_taxon)) %>% | ||
dplyr::group_by(sample) %>% | ||
dplyr::summarise(Taxons_inconnus = paste(cd_taxon,collapse = ",")) | ||
|
||
taxons_halins <- subset(df, cd_taxon %in% subset(table_metrics,halin == 1)$cd_taxon) %>% | ||
dplyr::group_by(sample) %>% | ||
dplyr::summarise(Taxons_halins = paste(cd_taxon,collapse = ",")) | ||
|
||
|
||
# Ajout des taxons manquants | ||
if(nrow(taxons_missing>0)) { | ||
|
||
newlines <- suppressWarnings(taxons_missing %>% | ||
dplyr::select(cd_taxon = Taxons_inconnus) %>% | ||
dplyr::bind_rows(table_metrics) %>% | ||
dplyr::mutate_at(dplyr::vars(indiciel:NH4), | ||
dplyr::funs(ifelse(is.na(.), 0, .)))) | ||
} | ||
|
||
|
||
df_standard <- df %>% | ||
dplyr::left_join(transcode, by = c("cd_taxon"="code")) %>% | ||
dplyr::select(-cd_taxon) %>% | ||
dplyr::group_by(sample,IDGF_v1) %>% | ||
dplyr::summarise(abondance = sum(abondance, na.rm=TRUE)) %>% | ||
dplyr::ungroup() %>% | ||
dplyr::group_by(sample) %>% | ||
dplyr::mutate(n_valves = sum(abondance)) %>% | ||
dplyr::ungroup() %>% | ||
dplyr::rowwise() %>% | ||
dplyr::mutate(abondance = round(abondance / n_valves * 400)) %>% | ||
dplyr::ungroup() %>% | ||
dplyr::select(sample,cd_taxon = IDGF_v1,abondance) | ||
|
||
|
||
res <- list(dataset = df_standard, metadata = tab_her, taxons_missing = taxons_missing, taxons_halins = taxons_halins) | ||
|
||
return(res) | ||
|
||
} |
Oops, something went wrong.