From 809da4a39814d923c48a004fb5c843670fe66974 Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Mon, 22 Apr 2024 15:04:35 -0500 Subject: [PATCH] simplify file structure renames files that define an `adjust_*()` function and associated method as `adjust_function_name.R`. helps to differentiate this relatively homogenous file type from other kinds of source files. --- ...uivocal-zone.R => adjust-equivocal-zone.R} | 0 ...bration.R => adjust-numeric-calibration.R} | 0 R/{numeric-range.R => adjust-numeric-range.R} | 0 ...s-custom.R => adjust-predictions-custom.R} | 0 ...ion.R => adjust-probability-calibration.R} | 0 ...shold.R => adjust-probability-threshold.R} | 0 R/container.R | 2 +- R/operation.R | 29 ------------------ R/utils.R | 30 +++++++++++++++++++ R/validation-rules.R | 12 ++++---- man/adjust_equivocal_zone.Rd | 2 +- man/adjust_numeric_calibration.Rd | 2 +- man/adjust_numeric_range.Rd | 2 +- man/adjust_predictions_custom.Rd | 2 +- man/adjust_probability_calibration.Rd | 2 +- man/adjust_probability_threshold.Rd | 2 +- 16 files changed, 43 insertions(+), 42 deletions(-) rename R/{equivocal-zone.R => adjust-equivocal-zone.R} (100%) rename R/{numeric-calibration.R => adjust-numeric-calibration.R} (100%) rename R/{numeric-range.R => adjust-numeric-range.R} (100%) rename R/{predictions-custom.R => adjust-predictions-custom.R} (100%) rename R/{probability-calibration.R => adjust-probability-calibration.R} (100%) rename R/{prob-threshold.R => adjust-probability-threshold.R} (100%) delete mode 100644 R/operation.R diff --git a/R/equivocal-zone.R b/R/adjust-equivocal-zone.R similarity index 100% rename from R/equivocal-zone.R rename to R/adjust-equivocal-zone.R diff --git a/R/numeric-calibration.R b/R/adjust-numeric-calibration.R similarity index 100% rename from R/numeric-calibration.R rename to R/adjust-numeric-calibration.R diff --git a/R/numeric-range.R b/R/adjust-numeric-range.R similarity index 100% rename from R/numeric-range.R rename to R/adjust-numeric-range.R diff --git a/R/predictions-custom.R b/R/adjust-predictions-custom.R similarity index 100% rename from R/predictions-custom.R rename to R/adjust-predictions-custom.R diff --git a/R/probability-calibration.R b/R/adjust-probability-calibration.R similarity index 100% rename from R/probability-calibration.R rename to R/adjust-probability-calibration.R diff --git a/R/prob-threshold.R b/R/adjust-probability-threshold.R similarity index 100% rename from R/prob-threshold.R rename to R/adjust-probability-threshold.R diff --git a/R/container.R b/R/container.R index 627c877..0b770d2 100644 --- a/R/container.R +++ b/R/container.R @@ -57,7 +57,7 @@ new_container <- function(mode, type, operations, columns, ptype, call) { } # validate operation order and check duplicates - validate_oper_order(operations, mode, call) + validate_order(operations, mode, call) # check columns res <- list( diff --git a/R/operation.R b/R/operation.R deleted file mode 100644 index dee6d76..0000000 --- a/R/operation.R +++ /dev/null @@ -1,29 +0,0 @@ -# These values are used to specify "what will we need for the adjustment?" and -# "what will we change?". For the outputs, we cannot change the probabilities -# without changing the classes. This is important because we are going to have -# to define constrains on the order of adjustments. - -input_vals <- c("numeric", "probability", "class", "everything") -output_vals <- c("numeric", "probability_class", "class", "everything") - -new_operation <- function(cls, inputs, outputs, arguments, results = list(trained = FALSE), ...) { - inputs <- arg_match0(inputs, input_vals) - outputs <- arg_match0(outputs, output_vals) - - if (!any(names(results) == "trained")) { - cli_abort("The {.arg results} slot requires a logical variable called \\ - {.val trained}") - } else { - check_logical(results$trained) - } - - res <- - list( - inputs = inputs, - outputs = outputs, - arguments = arguments, - results = results - ) - class(res) <- c(cls, "operation") - res -} diff --git a/R/utils.R b/R/utils.R index 3f34f2b..bcd1464 100644 --- a/R/utils.R +++ b/R/utils.R @@ -15,3 +15,33 @@ no_param <- component = character(0), component_id = character(0) ) + +# These values are used to specify "what will we need for the adjustment?" and +# "what will we change?". For the outputs, we cannot change the probabilities +# without changing the classes. This is important because we are going to have +# to define constrains on the order of adjustments. + +input_vals <- c("numeric", "probability", "class", "everything") +output_vals <- c("numeric", "probability_class", "class", "everything") + +new_operation <- function(cls, inputs, outputs, arguments, results = list(trained = FALSE), ...) { + inputs <- arg_match0(inputs, input_vals) + outputs <- arg_match0(outputs, output_vals) + + if (!any(names(results) == "trained")) { + cli_abort("The {.arg results} slot requires a logical variable called \\ + {.val trained}") + } else { + check_logical(results$trained) + } + + res <- + list( + inputs = inputs, + outputs = outputs, + arguments = arguments, + results = results + ) + class(res) <- c(cls, "operation") + res +} diff --git a/R/validation-rules.R b/R/validation-rules.R index 4402c63..a6a74a7 100644 --- a/R/validation-rules.R +++ b/R/validation-rules.R @@ -1,5 +1,5 @@ -validate_oper_order <- function(ops, mode, call) { - oper_data <- +validate_order <- function(ops, mode, call) { + orderings <- tibble::new_tibble(list( name = purrr::map_chr(ops, ~ class(.x)[1]), input = purrr::map_chr(ops, ~ .x$inputs), @@ -10,16 +10,16 @@ validate_oper_order <- function(ops, mode, call) { )) if (length(ops) < 2) { - return(invisible(oper_data)) + return(invisible(orderings)) } if (mode == "classification") { - check_classification_order(oper_data, call) + check_classification_order(orderings, call) } else { - check_regression_order(oper_data, call) + check_regression_order(orderings, call) } - invisible(oper_data) + invisible(orderings) } check_classification_order <- function(x, call) { diff --git a/man/adjust_equivocal_zone.Rd b/man/adjust_equivocal_zone.Rd index 3967dbc..705f41e 100644 --- a/man/adjust_equivocal_zone.Rd +++ b/man/adjust_equivocal_zone.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/equivocal-zone.R +% Please edit documentation in R/adjust-equivocal-zone.R \name{adjust_equivocal_zone} \alias{adjust_equivocal_zone} \title{Apply an equivocal zone to a binary classification model.} diff --git a/man/adjust_numeric_calibration.Rd b/man/adjust_numeric_calibration.Rd index 2244960..6c13706 100644 --- a/man/adjust_numeric_calibration.Rd +++ b/man/adjust_numeric_calibration.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/numeric-calibration.R +% Please edit documentation in R/adjust-numeric-calibration.R \name{adjust_numeric_calibration} \alias{adjust_numeric_calibration} \title{Re-calibrate numeric predictions} diff --git a/man/adjust_numeric_range.Rd b/man/adjust_numeric_range.Rd index a611687..08911f8 100644 --- a/man/adjust_numeric_range.Rd +++ b/man/adjust_numeric_range.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/numeric-range.R +% Please edit documentation in R/adjust-numeric-range.R \name{adjust_numeric_range} \alias{adjust_numeric_range} \title{Truncate the range of numeric predictions} diff --git a/man/adjust_predictions_custom.Rd b/man/adjust_predictions_custom.Rd index 1a1e417..4f54ced 100644 --- a/man/adjust_predictions_custom.Rd +++ b/man/adjust_predictions_custom.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/predictions-custom.R +% Please edit documentation in R/adjust-predictions-custom.R \name{adjust_predictions_custom} \alias{adjust_predictions_custom} \title{Change or add variables} diff --git a/man/adjust_probability_calibration.Rd b/man/adjust_probability_calibration.Rd index d71f1c0..65e0392 100644 --- a/man/adjust_probability_calibration.Rd +++ b/man/adjust_probability_calibration.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/probability-calibration.R +% Please edit documentation in R/adjust-probability-calibration.R \name{adjust_probability_calibration} \alias{adjust_probability_calibration} \title{Re-calibrate classification probability predictions} diff --git a/man/adjust_probability_threshold.Rd b/man/adjust_probability_threshold.Rd index 30e2af6..ea227dd 100644 --- a/man/adjust_probability_threshold.Rd +++ b/man/adjust_probability_threshold.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/prob-threshold.R +% Please edit documentation in R/adjust-probability-threshold.R \name{adjust_probability_threshold} \alias{adjust_probability_threshold} \title{Change the event threshold}