File size: 12,352 Bytes
87c83ab
 
75d3e9f
 
 
 
cb63bef
9856d69
889ab6a
87c83ab
889ab6a
 
 
 
 
 
 
 
cb63bef
 
 
 
889ab6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c38363b
 
 
 
889ab6a
 
cb63bef
 
 
 
889ab6a
87c83ab
 
889ab6a
87c83ab
 
cb63bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87c83ab
 
 
 
 
 
 
889ab6a
87c83ab
 
889ab6a
87c83ab
889ab6a
87c83ab
 
c38363b
 
 
 
 
 
 
 
 
87c83ab
889ab6a
 
 
 
 
 
 
 
 
 
87c83ab
 
cb63bef
 
 
 
 
621a5a4
cb63bef
 
621a5a4
cb63bef
 
 
87c83ab
 
889ab6a
87c83ab
 
 
889ab6a
87c83ab
889ab6a
 
 
 
 
87c83ab
 
 
889ab6a
87c83ab
 
 
 
889ab6a
 
 
 
 
cb63bef
 
 
87c83ab
 
 
cb63bef
 
 
 
87c83ab
889ab6a
 
87c83ab
cb63bef
 
 
 
 
87c83ab
889ab6a
 
 
 
 
 
cb63bef
889ab6a
 
 
 
 
 
 
 
 
87c83ab
 
 
 
889ab6a
87c83ab
 
 
 
 
 
889ab6a
87c83ab
 
 
 
 
cb63bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8cfd7b
 
 
 
 
9856d69
 
87c83ab
073e2cd
 
 
 
 
 
 
 
5424f55
 
 
 
 
 
 
 
8dd9b7b
 
 
ed3da92
 
 
 
 
 
 
8dd9b7b
ed3da92
8dd9b7b
 
 
846ae7b
 
 
 
8dd9b7b
 
 
889ab6a
 
 
 
c38363b
889ab6a
cb63bef
 
 
 
 
 
 
 
3de6a56
cb63bef
f8cfd7b
 
 
073e2cd
 
f8cfd7b
cb63bef
f8cfd7b
 
cb63bef
 
5424f55
cb63bef
 
 
 
f8cfd7b
 
 
 
889ab6a
 
 
 
 
 
 
 
 
 
f8cfd7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
889ab6a
 
 
 
cb63bef
 
 
889ab6a
 
 
 
621a5a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e29363a
 
621a5a4
 
 
 
889ab6a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env Rscript
library(optparse)
suppressWarnings(suppressMessages(library(cvms)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(jsonlite)))

dev_mode <- FALSE

option_list <- list(
    make_option(c("--data_path"),
        type = "character",
        help = "Path to data file (.csv)."
    ),
    make_option(c("--out_path"),
        type = "character",
        help = "Path to save confusion matrix plot at."
    ),
    make_option(c("--settings_path"),
        type = "character",
        help = "Path to get design settings from. Should be a .json file."
    ),
    make_option(c("--data_are_counts"),
        action = "store_true", default = FALSE,
        help = "Indicates that `--data_path` contains counts, not predictions."
    ),
    make_option(c("--target_col"),
        type = "character",
        help = "Target column"
    ),
    make_option(c("--prediction_col"),
        type = "character",
        help = "Prediction column"
    ),
    make_option(c("--n_col"),
        type = "character",
        help = "Count column (when `--data_are_counts`)."
    ),
    make_option(c("--sub_col"),
        type = "character",
        help = "Sub column (when `--data_are_counts`)."
    ),
    make_option(c("--classes"),
        type = "character",
        help = paste0(
            "Comma-separated class names. ",
            "Only these classes will be used - in the specified order."
        )
    )
)

opt_parser <- OptionParser(option_list = option_list)
opt <- parse_args(opt_parser)

design_settings <- tryCatch(
    {
        read_json(path = opt$settings_path)
    },
    error = function(e) {
        print(paste0(
            "Failed to read design settings as a json file ",
            opt$settings_path
        ))
        print(e)
        stop(e)
    }
)

if (isTRUE(dev_mode)) {
    print("Arguments:")
    print(opt)
    print(design_settings)
}

data_are_counts <- opt$data_are_counts

# read.csv turns white space into dots
target_col <- stringr::str_squish(opt$target_col)
target_col <- stringr::str_replace_all(target_col, " ", ".")
prediction_col <- stringr::str_squish(opt$prediction_col)
prediction_col <- stringr::str_replace_all(prediction_col, " ", ".")

n_col <- NULL
if (!is.null(opt$n_col)) {
    n_col <- stringr::str_squish(opt$n_col)
    n_col <- stringr::str_replace_all(n_col, " ", ".")
}

sub_col <- NULL
if (!is.null(opt$sub_col)) {
    if (!data_are_counts) {
        stop("`sub_col` can only be specified when data are counts.")
    }
    sub_col <- stringr::str_squish(opt$sub_col)
    sub_col <- stringr::str_replace_all(sub_col, " ", ".")
}

# Read and prepare data frame
df <- tryCatch(
    {
        read.csv(opt$data_path)
    },
    error = function(e) {
        print(paste0("Failed to read data from ", opt$data_path))
        print(e)
        stop(e)
    }
)

df <- dplyr::as_tibble(df)

if (isTRUE(dev_mode)) {
    print(df)
}

if (!target_col %in% colnames(df)) {
    stop("Specified `target_col` not a column in the data.")
}
if (!prediction_col %in% colnames(df)) {
    stop("Specified `target_col` not a column in the data.")
}

df[[target_col]] <- as.character(df[[target_col]])

if (isTRUE(data_are_counts)) {
    df[[prediction_col]] <- as.character(df[[prediction_col]])
}

# Predictions can be either probabilities or
# hard class predictions
if (is.integer(df[[prediction_col]]) || !is.numeric(df[[prediction_col]])) {
    all_present_classes <- sort(
        c(
            unique(df[[target_col]]),
            unique(df[[prediction_col]])
        )
    )
} else {
    all_present_classes <- sort(
        unique(df[[target_col]])
    )
}

if (!is.null(opt$classes)) {
    classes <- as.character(
        unlist(strsplit(opt$classes, "[,:]")),
        recursive = TRUE
    )
    if (length(setdiff(classes, all_present_classes)) > 0) {
        stop("One or more specified classes are not in the data set.")
    }
} else {
    classes <- all_present_classes
}

if (isTRUE(dev_mode)) {
    print(paste0("Selected Classes: ", paste0(classes, collapse = ", ")))
}

if (!isTRUE(data_are_counts)) {
    # We remove the unwanted classes from the confusion matrix
    # (easier - possibly slower in edge cases)
    family <- ifelse(
        length(all_present_classes) == 2,
        "binomial",
        "multinomial"
    )

    evaluation <- tryCatch(
        {
            cvms::evaluate(
                data = df,
                target_col = target_col,
                prediction_cols = prediction_col,
                type = family
            )
        },
        error = function(e) {
            print("Failed to evaluate data.")
            print(head(df, 5))
            print(e)
            stop(e)
        }
    )

    confusion_matrix <- evaluation[["Confusion Matrix"]][[1]]
} else {
    confusion_matrix <- dplyr::rename(
        df,
        Target = !!target_col,
        Prediction = !!prediction_col,
        N = !!n_col
    )
}

confusion_matrix <- dplyr::filter(
    confusion_matrix,
    Prediction %in% classes,
    Target %in% classes
)

# Plotting settings

build_fontface <- function(bold, italic) {
    dplyr::case_when(
        isTRUE(bold) && isTRUE(italic) ~ "bold.italic",
        isTRUE(bold) ~ "bold",
        isTRUE(italic) ~ "italic",
        TRUE ~ "plain"
    )
}

top_font_args <- list(
    "size" = design_settings$font_top_size,
    "color" = design_settings$font_top_color,
    "fontface" = build_fontface(
        design_settings$font_top_bold,
        design_settings$font_top_italic
    ),
    "alpha" = design_settings$font_top_alpha
)

bottom_font_args <- list(
    "size" = design_settings$font_bottom_size,
    "color" = design_settings$font_bottom_color,
    "fontface" = build_fontface(
        design_settings$font_bottom_bold,
        design_settings$font_bottom_italic
    ),
    "alpha" = design_settings$font_bottom_alpha
)

percentages_font_args <- list(
    "size" = design_settings$font_percentage_size,
    "color" = design_settings$font_percentage_color,
    "fontface" = build_fontface(
        design_settings$font_percentage_bold,
        design_settings$font_percentage_italic
    ),
    "alpha" = design_settings$font_percentage_alpha,
    "prefix" = design_settings$font_percentage_prefix,
    "suffix" = design_settings$font_percentage_suffix
)

normalized_font_args <- list(
    "prefix" = design_settings$font_normalized_prefix,
    "suffix" = design_settings$font_normalized_suffix
)

counts_font_args <- list(
    "prefix" = design_settings$font_counts_prefix,
    "suffix" = design_settings$font_counts_suffix
)


if (isTRUE(design_settings$counts_on_top) ||
    !isTRUE(design_settings$show_normalized)) {
    # Counts on top!
    counts_font_args <- c(
        counts_font_args, top_font_args
    )
    normalized_font_args <- c(
        normalized_font_args, bottom_font_args
    )
} else {
    normalized_font_args <- c(
        normalized_font_args, top_font_args
    )
    counts_font_args <- c(
        counts_font_args, bottom_font_args
    )
}

tile_border_color <- NA
if (isTRUE(design_settings$show_tile_border)) {
    tile_border_color <- design_settings$tile_border_color
}

intensity_by <- tolower(design_settings$intensity_by)
if (grepl("normalized", intensity_by)) intensity_by <- "normalized"

intensity_lims <- NULL
if (isTRUE(design_settings$set_intensity_lims)) {
    intensity_lims <- c(
        design_settings$intensity_min,
        design_settings$intensity_max
    )
}

palette <- design_settings$palette
if (isTRUE(design_settings$palette_use_custom)) {
    palette <- list(
        "low" = design_settings$palette_custom_low,
        "high" = design_settings$palette_custom_high
    )
}

# Sum tiles
sums_settings <- sum_tile_settings()
if (isTRUE(design_settings$show_sums)) {
    sum_tile_palette <- design_settings$sum_tile_palette
    if (isTRUE(design_settings$sum_tile_palette_use_custom)) {
        sum_tile_palette <- list(
            "low" = design_settings$sum_tile_palette_custom_low,
            "high" = design_settings$sum_tile_palette_custom_high
        )
    }
    sums_settings <- sum_tile_settings(
        palette = sum_tile_palette,
        label = design_settings$sum_tile_label,
        tile_border_color = tile_border_color,
        tile_border_size = design_settings$tile_border_size,
        tile_border_linetype = design_settings$tile_border_linetype,
        tc_tile_border_color = tile_border_color,
        tc_tile_border_size = design_settings$tile_border_size,
        tc_tile_border_linetype = design_settings$tile_border_linetype
    )
}

confusion_matrix_plot <- tryCatch(
    {
        cvms::plot_confusion_matrix(
            confusion_matrix,
            sub_col = sub_col,
            class_order = classes,
            add_sums = design_settings$show_sums,
            add_counts = design_settings$show_counts,
            add_normalized = design_settings$show_normalized,
            add_row_percentages = design_settings$show_row_percentages,
            add_col_percentages = design_settings$show_col_percentages,
            rm_zero_percentages = !design_settings$show_zero_percentages,
            rm_zero_text = !design_settings$show_zero_text,
            add_zero_shading = design_settings$show_zero_shading,
            amount_3d_effect = as.integer(design_settings$amount_3d_effect),
            add_arrows = design_settings$show_arrows,
            arrow_size = design_settings$arrow_size,
            arrow_nudge_from_text = design_settings$arrow_nudge_from_text,
            intensity_by = intensity_by,
            intensity_lims = intensity_lims,
            intensity_beyond_lims = design_settings$intensity_beyond_lims,
            darkness = design_settings$darkness,
            counts_on_top = design_settings$counts_on_top,
            place_x_axis_above = design_settings$place_x_axis_above,
            rotate_y_text = design_settings$rotate_y_text,
            diag_percentages_only = design_settings$diag_percentages_only,
            digits = as.integer(design_settings$num_digits),
            palette = palette,
            sums_settings = sums_settings,
            font_counts = do.call("font", counts_font_args),
            font_normalized = do.call("font", normalized_font_args),
            font_row_percentages = do.call("font", percentages_font_args),
            font_col_percentages = do.call("font", percentages_font_args),
            tile_border_color = tile_border_color,
            tile_border_size = design_settings$tile_border_size,
            tile_border_linetype = design_settings$tile_border_linetype
        )
    },
    error = function(e) {
        print("Failed to create plot from confusion matrix.")
        print(confusion_matrix)
        print(e)
        stop(e)
    }
)

# Add labels on x and y axes
confusion_matrix_plot <- confusion_matrix_plot +
    ggplot2::labs(
        x = design_settings$x_label,
        y = design_settings$y_label
    )

# Add title
if (nchar(design_settings$title_label) > 0) {
    confusion_matrix_plot <- confusion_matrix_plot +
        ggplot2::labs(
            title = design_settings$title_label
        )
}

# Add caption
if (nchar(design_settings$caption_label) > 0) {
    confusion_matrix_plot <- confusion_matrix_plot +
        ggplot2::labs(
            caption = design_settings$caption_label
        )
}


tryCatch(
    {
        ggplot2::ggsave(
            opt$out_path,
            width = design_settings$width,
            height = design_settings$height,
            dpi = design_settings$dpi,
            units = "px"
        )
    },
    error = function(e) {
        print(paste0("png: Failed to ggsave plot to: ", opt$out_path))
        print(e)
        stop(e)
    }
)

# Create a jpg version as well
tryCatch(
    {
        ggplot2::ggsave(
            paste0(substr(
                opt$out_path,
                start = 1,
                stop = nchar(opt$out_path) - 3
            ), "jpg"),
            width = design_settings$width,
            height = design_settings$height,
            dpi = design_settings$dpi,
            units = "px",
            bg = "white"
        )
    },
    error = function(e) {
        print(paste0("jpg: Failed to ggsave plot to: ", opt$out_path))
        print(e)
        stop(e)
    }
)