Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludvig committed Jun 10, 2023
2 parents c59f350 + c0aa86d commit cb1003d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: cvms
Title: Cross-Validation for Model Selection
Version: 1.4.0.9000
Version: 1.4.1
Authors@R:
c(person(given = "Ludvig Renbo",
family = "Olsen",
Expand Down
16 changes: 14 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@

# cvms 1.4.0.9000
# cvms 1.4.1

* In `plot_confusion_matrix()`, fixes bug when `add_sums = TRUE` and `counts_on_top = TRUE`.
## `plot_confusion_matrix()`:

* Adds option to set `intensity_by` to a log/arcsinh transformed version of the counts.
This adds the options `"log counts"`, `"log2 counts"`, `"log10 counts"`, `"arcsinh counts"`
to the `intensity_by` argument.

* Fixes bug when `add_sums = TRUE` and `counts_on_top = TRUE`.

* Raises error for negative counts.

* Fixes zero-division when all counts are 0.

* Sets palette colors to lowest value when all counts are 0.

# cvms 1.4.0

Expand Down
20 changes: 17 additions & 3 deletions R/plot_confusion_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,15 @@
#' @param font_col_percentages \code{list} of font settings for the column percentages.
#' Can be provided with \code{\link[cvms:font]{font()}}.
#' @param intensity_by The measure that should control the color intensity of the tiles.
#' Either \code{`counts`} or \code{`normalized`}. For the latter, the color limits become
#' \code{0-100}, why the intensities can better be compared across plots.
#' Either \code{`counts`}, \code{`normalized`} or one of \code{`log counts`,
#' `log2 counts`, `log10 counts`, `arcsinh counts`}.
#'
#' For `normalized`, the color limits become \code{0-100}, why the intensities
#' can better be compared across plots.
#'
#' For the `log*` and `arcsinh` versions, the log/arcsinh transformed counts are used.
#' \strong{Note}: In `log*` transformed counts, 0-counts are set to `0`, why they
#' won't be distinguishable from 1-counts.
#' @param arrow_size Size of arrow icons. (Numeric)
#'
#' Is divided by \code{sqrt(nrow(conf_matrix))} and passed on
Expand Down Expand Up @@ -371,7 +378,7 @@ plot_confusion_matrix <- function(conf_matrix,
)
checkmate::assert_names(
x = intensity_by,
subset.of = c("counts", "normalized"),
subset.of = c("counts", "normalized", "log counts", "log2 counts", "log10 counts", "arcsinh counts"),
add = assert_collection
)

Expand All @@ -392,6 +399,13 @@ plot_confusion_matrix <- function(conf_matrix,
"'palette' and 'sums_settings[['palette']]' cannot be the same palette.")
}

# Check that N are (>= 0)
checkmate::assert_numeric(
x = conf_matrix[[counts_col]],
lower = 0,
add = assert_collection
)

checkmate::reportAssertions(assert_collection)
# End of argument checks ####

Expand Down
43 changes: 32 additions & 11 deletions R/plotting_utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ update_font_setting <- function(settings, defaults, initial_vals = NULL) {

preprocess_numeric <- function(vec, settings, rm_zero_text=FALSE, rm_zeroes_post_rounding=TRUE) {

# Find the pre-rounding 0s
is_zero <- vec == 0
# Find the pre-rounding 0s or NaNs
is_zero <- vec == 0 | is.na(vec)

# Don't round if digits is negative
if (settings[["digits"]] >= 0) {
Expand All @@ -101,7 +101,7 @@ preprocess_numeric <- function(vec, settings, rm_zero_text=FALSE, rm_zeroes_post
# Potentially including elements zero after rounding
if (isTRUE(rm_zero_text)){
if (isTRUE(rm_zeroes_post_rounding)){
out[vec == 0] <- ""
out[vec == 0 | is.na(vec)] <- ""
} else {
out[is_zero] <- ""
}
Expand Down Expand Up @@ -228,24 +228,45 @@ get_figure_path <- function(fig_name, inst_dir = "images", pgk_name = "cvms") {


calculate_normalized <- function(data){
data[["Normalized"]] <- 100 * (data[["N"]] / sum(data[["N"]]))
sum_ <- sum(data[["N"]])
if (sum_ == 0){
sum_ <- 1
}
data[["Normalized"]] <- 100 * (data[["N"]] / sum_)
data
}

set_intensity <- function(data, intensity_by){
if (intensity_by == "counts") {
data[["Intensity"]] <- data[["N"]]
} else {
set_intensity <- function(data, intensity_by) {
if (grepl("counts", intensity_by)) {
counts <- data[["N"]]

if (intensity_by == "log counts") {
counts[counts != 0] <- log(counts[counts != 0])
} else if (intensity_by == "log2 counts") {
counts[counts != 0] <- log2(counts[counts != 0])
} else if (intensity_by == "log10 counts") {
counts[counts != 0] <- log10(counts[counts != 0])
} else if (intensity_by == "arcsinh counts") {
counts <- asinh(counts)
}

data[["Intensity"]] <- counts

} else if (intensity_by == "normalized") {
data[["Intensity"]] <- data[["Normalized"]]
}
data
}

get_intensity_range <- function(data, intensity_by){
# Get min and max intensity scores
if (intensity_by == "counts"){
min_intensity <- min(data$N)
max_intensity <- max(data$N)
if (grepl("counts", intensity_by) ){
min_intensity <- min(data$Intensity)
max_intensity <- max(data$Intensity)
if (min_intensity == max_intensity && min_intensity == 0){
# When all are 0, make sure all get lowest value in palette
max_intensity <- 1
}
} else {
min_intensity <- 0
max_intensity <- 100
Expand Down
11 changes: 9 additions & 2 deletions man/plot_confusion_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb1003d

Please sign in to comment.