Spaces:
Sleeping
Sleeping
Download helpers.R from riki74/equil2-api: direct link, hf CLI and curl.
- Browser
- Download file 7.45 kB
-
https://huggingface.co/spaces/riki74/equil2-api/resolve/main/helpers.R
- Command line
-
hf download hf://spaces/riki74/equil2-api/helpers.R
-
curl -L -o helpers.R https://huggingface.co/spaces/riki74/equil2-api/resolve/main/helpers.R
7.45 kB
| # VERSION: 2.4 - Fixed equil2 parameter names (2025-10-01) | |
| library(dplyr) | |
| library(tidyr) | |
| library(purrr) | |
| library(units) | |
| MW <- list( | |
| Ca = 40.078, Mg = 24.305, Na = 22.990, K = 39.098, NH4 = 18.039, | |
| Cl = 35.453, PO4 = 94.9714, SO4 = 96.06, Ox = 88.019, | |
| Citr = 192.124, Urate = 168.11 | |
| ) | |
| mgL_to_mmolL <- function(mgL_values, molecular_weight) { | |
| mgL_values / molecular_weight | |
| } | |
| normalize_records_to_df <- function(records) { | |
| df_raw <- jsonlite::fromJSON( | |
| jsonlite::toJSON(records, auto_unbox = TRUE), | |
| simplifyVector = TRUE, | |
| simplifyDataFrame = FALSE | |
| ) | |
| df <- as.data.frame(df_raw, stringsAsFactors = FALSE) | |
| for (col in names(df)) { | |
| df[[col]] <- as.numeric(unlist(df[[col]])) | |
| } | |
| return(df) | |
| } | |
| convert_and_validate <- function(df) { | |
| df <- df |> dplyr::rename_with(~gsub("\\s+", "", .x)) | |
| required_cols <- c("Na_mmolL", "K_mmolL", "NH4_mmolL", "Cl_mmolL", "SO4_mmolL", "pH") | |
| missing_required <- setdiff(required_cols, colnames(df)) | |
| if (length(missing_required) > 0) { | |
| stop(paste("Missing required columns:", paste(missing_required, collapse = ", "))) | |
| } | |
| convert_with_fallback <- function(df, col_mmol, col_mg, mw, min_value = 0) { | |
| if (col_mmol %in% names(df)) { | |
| vals <- as.numeric(unlist(df[[col_mmol]])) | |
| return(pmax(vals, min_value)) | |
| } else if (col_mg %in% names(df)) { | |
| vals <- as.numeric(unlist(df[[col_mg]])) | |
| return(pmax(mgL_to_mmolL(vals, mw), min_value)) | |
| } else { | |
| return(rep(min_value, nrow(df))) | |
| } | |
| } | |
| ca_values <- convert_with_fallback(df, "Ca_mmolL", "Ca_mgL", MW$Ca, min_value = 0) | |
| mg_values <- convert_with_fallback(df, "Mg_mmolL", "Mg_mgL", MW$Mg, min_value = 0.01) | |
| po4_values <- convert_with_fallback(df, "PO4_mmolL", "PO4_mgL", MW$PO4, min_value = 0) | |
| ox_values <- convert_with_fallback(df, "Ox_mmolL", "Ox_mgL", MW$Ox, min_value = 0) | |
| citr_values <- convert_with_fallback(df, "Citr_mmolL", "Citr_mgL", MW$Citr, min_value = 0.01) | |
| urate_values <- convert_with_fallback(df, "Urate_mmolL", "Urate_mgL", MW$Urate, min_value = 0.01) | |
| cols_to_remove <- grep("_mgL$", names(df), value = TRUE) | |
| df <- df |> select(-any_of(cols_to_remove)) | |
| optional_mmol_cols <- c("Ca_mmolL", "Mg_mmolL", "PO4_mmolL", "Ox_mmolL", "Citr_mmolL", "Urate_mmolL") | |
| df <- df |> select(-any_of(optional_mmol_cols)) | |
| df_converted <- df |> | |
| mutate( | |
| Ca_mmolL = ca_values, | |
| Mg_mmolL = mg_values, | |
| PO4_mmolL = po4_values, | |
| Ox_mmolL = ox_values, | |
| Citr_mmolL = citr_values, | |
| Urate_mmolL = urate_values | |
| ) | |
| for (col in names(df_converted)) { | |
| df_converted[[col]] <- as.numeric(unlist(df_converted[[col]])) | |
| } | |
| if (any(!is.finite(df_converted$pH) | df_converted$pH < 3 | df_converted$pH > 9, na.rm = TRUE)) { | |
| stop("Invalid pH values detected. Expected range: 4.0-8.5") | |
| } | |
| conc_cols <- c("Na_mmolL", "K_mmolL", "Ca_mmolL", "Mg_mmolL", "NH4_mmolL", | |
| "Cl_mmolL", "PO4_mmolL", "SO4_mmolL", "Ox_mmolL", "Citr_mmolL", "Urate_mmolL") | |
| for (col in conc_cols) { | |
| if (col %in% names(df_converted)) { | |
| if (any(df_converted[[col]] < 0, na.rm = TRUE)) { | |
| stop(paste("Negative concentrations detected in column:", col)) | |
| } | |
| } | |
| } | |
| return(df_converted) | |
| } | |
| # WRAPPER corretto per equil2 - usa nomi parametri corretti dalla documentazione | |
| call_equil2_with_units <- function(Na, K, Ca, Mg, NH4, Cl, PO4, SO4, C2O4, Citrate, Uric, pH) { | |
| # equil2 usa nomi specifici: sodium_mEq_L, potassium_mEq_L, calcium_mg_dL, etc. | |
| # Conversioni: mmol/L -> unità richieste da equil2 | |
| result <- equil2::equil2( | |
| sodium_mEq_L = units::set_units(Na, "mmol_sodium/L"), | |
| potassium_mEq_L = units::set_units(K, "mmol_potassium/L"), | |
| calcium_mg_dL = units::set_units(Ca * MW$Ca / 10, "mg_calcium/dL"), | |
| magnesium_mg_dL = units::set_units(Mg * MW$Mg / 10, "mg_magnesium/dL"), | |
| ammonia_mEq_L = units::set_units(NH4, "mmol_ammonia/L"), | |
| chloride_mEq_L = units::set_units(Cl, "mmol_chloride/L"), | |
| phosphate_mg_dL = units::set_units(PO4 * MW$PO4 / 10, "mg_phosphate/dL"), | |
| sulfate_mg_dL = units::set_units(SO4 * MW$SO4 / 10, "mg_sulfate/dL"), | |
| oxalate_mg_dL = units::set_units(C2O4 * MW$Ox / 10, "mg_oxalate/dL"), | |
| citrate_mg_dL = units::set_units(Citrate * MW$Citr / 10, "mg_citrate/dL"), | |
| urate_mg_dL = units::set_units(Uric * MW$Urate / 10, "mg_urate/dL"), | |
| pH = pH | |
| ) | |
| return(result) | |
| } | |
| run_equil2_dataframe <- function(df_mmol) { | |
| if (nrow(df_mmol) == 0) { | |
| return(list(n = 0, success_count = 0, records = list())) | |
| } | |
| if (nrow(df_mmol) > 1000) { | |
| stop("Too many records. Maximum 1000 records per request.") | |
| } | |
| results <- vector("list", nrow(df_mmol)) | |
| for (i in seq_len(nrow(df_mmol))) { | |
| # Estrai valori come scalari numerici | |
| Na <- as.numeric(df_mmol$Na_mmolL[i]) | |
| K <- as.numeric(df_mmol$K_mmolL[i]) | |
| Ca <- as.numeric(df_mmol$Ca_mmolL[i]) | |
| Mg <- as.numeric(df_mmol$Mg_mmolL[i]) | |
| NH4 <- as.numeric(df_mmol$NH4_mmolL[i]) | |
| Cl <- as.numeric(df_mmol$Cl_mmolL[i]) | |
| PO4 <- as.numeric(df_mmol$PO4_mmolL[i]) | |
| SO4 <- as.numeric(df_mmol$SO4_mmolL[i]) | |
| C2O4 <- as.numeric(df_mmol$Ox_mmolL[i]) | |
| Citrate <- as.numeric(df_mmol$Citr_mmolL[i]) | |
| Uric <- as.numeric(df_mmol$Urate_mmolL[i]) | |
| pH_val <- as.numeric(df_mmol$pH[i]) | |
| # Validazione | |
| if (any(is.na(c(Na, K, Ca, Mg, NH4, Cl, PO4, SO4, C2O4, Citrate, Uric, pH_val)))) { | |
| results[[i]] <- list( | |
| SS_CaOx = NA_real_, SS_Brushite = NA_real_, | |
| SS_HAp = NA_real_, SS_UricAcid = NA_real_, | |
| DG_CaOx = NA_real_, DG_Brushite = NA_real_, | |
| DG_HAp = NA_real_, DG_UricAcid = NA_real_, | |
| status = "error", error_message = "NA values detected in input" | |
| ) | |
| next | |
| } | |
| # Chiamata equil2 con units wrapper | |
| results[[i]] <- tryCatch({ | |
| equil_result <- call_equil2_with_units(Na, K, Ca, Mg, NH4, Cl, PO4, SO4, C2O4, Citrate, Uric, pH_val) | |
| # equil_result è un dataframe con colonne: species, super_saturation, neg_delta_Gibbs | |
| ss_map <- setNames(equil_result$super_saturation, equil_result$species) | |
| dg_map <- setNames(equil_result$neg_delta_Gibbs, equil_result$species) | |
| list( | |
| SS_CaOx = as.numeric(ss_map["Calcium Oxalate"]), | |
| SS_Brushite = as.numeric(ss_map["Brushite"]), | |
| SS_HAp = as.numeric(ss_map["Hydroxyapatite"]), | |
| SS_UricAcid = as.numeric(ss_map["Uric Acid"]), | |
| DG_CaOx = as.numeric(dg_map["Calcium Oxalate"]), | |
| DG_Brushite = as.numeric(dg_map["Brushite"]), | |
| DG_HAp = as.numeric(dg_map["Hydroxyapatite"]), | |
| DG_UricAcid = as.numeric(dg_map["Uric Acid"]), | |
| status = "success", | |
| error_message = NA_character_ | |
| ) | |
| }, error = function(e) { | |
| list( | |
| SS_CaOx = NA_real_, SS_Brushite = NA_real_, | |
| SS_HAp = NA_real_, SS_UricAcid = NA_real_, | |
| DG_CaOx = NA_real_, DG_Brushite = NA_real_, | |
| DG_HAp = NA_real_, DG_UricAcid = NA_real_, | |
| status = "error", error_message = conditionMessage(e) | |
| ) | |
| }) | |
| } | |
| results_df <- bind_rows(results) | |
| output_df <- bind_cols(df_mmol, results_df) | |
| final_results <- jsonlite::fromJSON( | |
| jsonlite::toJSON(output_df, dataframe = "rows", auto_unbox = TRUE) | |
| ) | |
| success_count <- sum(results_df$status == "success", na.rm = TRUE) | |
| return(list( | |
| n = nrow(output_df), | |
| success_count = success_count, | |
| records = final_results, | |
| computation_time = as.character(Sys.time()) | |
| )) | |
| } |