Skip to content

Commit

Permalink
Lint (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Oct 28, 2024
1 parent 41529f9 commit 9c60c3b
Show file tree
Hide file tree
Showing 55 changed files with 760 additions and 728 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check-bioc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ jobs:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, r: 'release', bioc: '3.18', cont: "bioconductor/bioconductor_docker:RELEASE_3_18", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: 'release', bioc: '3.18' }
- { os: windows-latest, r: 'release', bioc: '3.18' }
- { os: ubuntu-latest, r: 'release', bioc: '3.19', cont: "bioconductor/bioconductor_docker:RELEASE_3_19", rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest" }
- { os: macOS-latest, r: 'release', bioc: '3.19' }
- { os: windows-latest, r: 'release', bioc: '3.19' }
## Check https://github.com/r-lib/actions/tree/master/examples
## for examples using the http-user-agent

Expand Down
7 changes: 7 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters: linters_with_defaults( # see vignette("lintr")
whitespace_linter = NULL,
indentation_linter = NULL,
line_length_linter = NULL,
quotes_linter = quotes_linter("'")
)
encoding: "UTF-8"
17 changes: 17 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"/usr/include/**",
"/usr/lib/R/library/*/include",
"${workspaceFolder}/**",
],
"defines": [],
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-clang-x64",
},
],
"version": 4,
}
34 changes: 34 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"cSpell.words": [
"axtype",
"bbox",
"bgplot",
"Biobase",
"colorlegend",
"Colour",
"colourbar",
"consec",
"diffusionmap",
"dists",
"Expl",
"ggplot",
"ggthemes",
"ggtitle",
"gradientn",
"idxs",
"igraph",
"lvls",
"nomap",
"nticks",
"pheno",
"plothelpers",
"rangeframe",
"rdname",
"rlang",
"scatterplot",
"vals",
"xlen",
"ylen",
"zlen"
],
}
11 changes: 5 additions & 6 deletions R/accessor-generics.r
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' destiny generics
#'
#'
#' destiny provides several generic methods and implements them for the \code{\link{DiffusionMap}} and \code{\link{Sigmas}} classes.
#'
#'
#' @param object Object from which to extract or to which to assign a value
#' @param value Value to assign within an object
#'
#'
#' @examples
#' data(guo_norm)
#' dm <- DiffusionMap(guo_norm)
Expand All @@ -14,9 +14,9 @@
#' optimal_sigma(dm)
#' dataset(dm)
#' distance(dm)
#'
#'
#' @seealso \link{DiffusionMap methods} and \link{Sigmas} class for implementations
#'
#'
#' @importFrom methods setGeneric
#' @name destiny generics
#' @rdname destiny-generics
Expand Down Expand Up @@ -77,4 +77,3 @@ setGeneric('distance<-', function(object, value) standardGeneric('distance<-'))
#' @rdname destiny-generics
#' @export
setGeneric('optimal_sigma', function(object) standardGeneric('optimal_sigma'))

42 changes: 21 additions & 21 deletions R/censoring.r
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
censoring <- function(data, sigma, dists, censor_val = NULL, censor_range = NULL, missing_range = NULL, callback = invisible) {
if (!is.null(censor_range))
censor_range <- matrix(censor_range, ncol = 2)

if (!is.null(missing_range))
missing_range <- matrix(missing_range, ncol = 2)

validate_censoring(data, sigma, dists, censor_val, censor_range, missing_range)

data <- as.matrix(data)

# dists is a sparse symmetrix matrix, which eigen cannot handle
# so we just pass its filled triangle as sparse matrix and convert it back afterwards
uplo <- dists@uplo
Expand All @@ -17,52 +17,52 @@ censoring <- function(data, sigma, dists, censor_val = NULL, censor_range = NULL
}

predict_censoring <- function(data, data2, censor_val = NULL, censor_range = NULL, missing_range = NULL, sigma) {
if (is.null(censor_val )) censor_val <- NA # this works since this will be NaN in C++ and comparison with NaN is always false
if (is.null(censor_range )) censor_range <- c(NA, NA)
if (is.null(censor_val)) censor_val <- NA # this works since this will be NaN in C++ and comparison with NaN is always false
if (is.null(censor_range)) censor_range <- c(NA, NA)
if (is.null(missing_range)) missing_range <- c(NA, NA)

predict_censoring_impl(data, data2, censor_val, censor_range, missing_range, sigma)
}

#' @importFrom Matrix sparseMatrix
validate_censoring <- function(data, sigma, dists, censor_val, censor_range, missing_range) {
G <- ncol(data)
validate_censoring <- function(data, sigma, dists, censor_val, censor_range, missing_range) { # nolint: cyclocomp_linter.
g <- ncol(data)
n <- nrow(data)

if (any(is.na(data)) && is.null(missing_range))
stop('Your data contains missing values (NA). You have to provide a the `missing_range` parameter.')

if (is.null(censor_val) != is.null(censor_range))
stop('You have to provide both a censoring value and a censor_range or none')

if (!is.null(censor_range)) {
if (!is.numeric(censor_val) || (length(censor_val) != 1L && length(censor_val) != G))
if (!is.numeric(censor_val) || (length(censor_val) != 1L && length(censor_val) != g))
stop('censor_val has to be a single numeric value, or length(censor_val) == ncol(data) must be TRUE')
if (!is.numeric(censor_range) || !(nrow(censor_range) %in% c(G, 1L)) || ncol(censor_range) != 2L || any(diff(t(censor_range)) <= 0L))

if (!is.numeric(censor_range) || !(nrow(censor_range) %in% c(g, 1L)) || ncol(censor_range) != 2L || any(diff(t(censor_range)) <= 0L))
stop('censor_range has to be a numeric vector of length 2, the second of which being larger,
or a matrix with nrow(censor_range) == ncol(data) where each row is such a vector')
}

if (!is.null(missing_range)) {
if (!is.numeric(missing_range) || !(nrow(missing_range) %in% c(G, 1L)) || ncol(missing_range) != 2L || any(diff(t(missing_range)) <= 0L))
if (!is.numeric(missing_range) || !(nrow(missing_range) %in% c(g, 1L)) || ncol(missing_range) != 2L || any(diff(t(missing_range)) <= 0L))
stop('missing_range has to be a numeric vector of length 2, the second of which being larger,
or a matrix with nrow(missing_range) == ncol(data) where each row is such a vector')
}

if (!is.numeric(sigma) || !length(sigma) %in% c(n, 1L))
stop('sigma has to be a single numeric value or of length nrow(data)')

if (!is(dists, 'dsCMatrix'))
stop('dists has to be a dsCMatrix, not ', class(dists))
}

test_censoring <- function(censor_val, censor_range, data, missing_range) {
if (is.null(data)) return(FALSE)

has_censor_range <- !(missing(censor_range) || is.null(censor_range))
has_missing_range <- !(missing(missing_range) || is.null(missing_range))
if (has_censor_range || has_missing_range) return(TRUE)

any(is.na(data)) || any(data == censor_val)
}
75 changes: 37 additions & 38 deletions R/colorlegend.r
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Color legend
#'
#'
#' Creates a color legend for a vector used to color a plot. It will use the current \code{\link[grDevices]{palette}()} or the specified \code{pal} as reference.
#'
#'
#' When passed a factor or integer vector, it will create a discrete legend, whereas a double vector will result in a continuous bar.
#'
#'
#' @param col Vector of factor, integer, or double used to determine the ticks.
#' @param pal If \code{col} is double, pal is used as a continuous palette, else as categorical one
#' @param log Use logarithmic scale?
Expand All @@ -20,19 +20,19 @@
#' @param left logical. If TRUE, invert posx
#' @param ... Additional parameters for the \link[graphics]{text} call used for labels
#' @param cex.main,cex.axis,col.main,col.lab For compatibility with \code{\link{par}}
#'
#'
#' @return This function is called for the side effect of adding a colorbar to a plot and returns nothing/NULL.
#'
#'
#' @examples
#' color_data <- 1:6
#' par(mar = par('mar') + c(0, 0, 0, 3))
#' plot(sample(6), col = color_data)
#' colorlegend(color_data)
#'
#'
#' @importFrom graphics par rect segments text
#' @importFrom grDevices colorRampPalette palette
#' @export
colorlegend <- function(
colorlegend <- function( # nolint: cyclocomp_linter.
col, pal = palette(), log = FALSE,
posx = c(.9, .93), posy = c(.05, .9),
main = NULL, cex_main = par('cex.sub'),
Expand All @@ -41,93 +41,92 @@ colorlegend <- function(
steps = 5, steps_color = 100,
digit = 2, left = FALSE,
...,
cex.main = NULL,
cex.axis = NULL,
col.main = NULL,
col.lab = NULL) {
cex.main = NULL, # nolint: object_name_linter.
cex.axis = NULL, # nolint: object_name_linter.
col.main = NULL, # nolint: object_name_linter.
col.lab = NULL) { # nolint: object_name_linter.
draw_ticks <- as.logical(steps)
if (!draw_ticks) steps <- 2L
if (!is.null(cex.main)) cex_main <- cex.main
if (!is.null(cex.axis)) cex_axis <- cex.axis
if (!is.null(col.main)) col_main <- col.main
if (!is.null(col.lab)) col_lab <- col.lab

zval <-
if (is.double(col)) seq(min(col, na.rm = TRUE), max(col, na.rm = TRUE), length.out = steps)
else if (is.factor(col)) factor(levels(col))
else sort(unique(col))

zval_num <-
if (is.integer(zval)) seq_along(zval)
else if (is.numeric(zval)) zval
else if (is.factor(zval) || is.character(zval)) seq_along(zval)
else as.integer(zval)

zlim <-
if (is.double(col)) range(zval_num)
else c(min(zval_num) - .5, max(zval_num) + .5)

par(new = TRUE)
omar <- nmar <- par('mar')
nmar[c(2, 4)] <- 0
par(mar = nmar)

emptyplot()

pars <- par('usr')
dx <- pars[[2]] - pars[[1]]
xmin <- pars[[1]] + posx[[1]] * dx
xmax <- pars[[1]] + posx[[2]] * dx
dy <- pars[[4]] - pars[[3]]
ymin <- pars[[3]] + posy[[1]] * dy
ymax <- pars[[3]] + posy[[2]] * dy

if (log) {
zlim <- log10(zlim)
zval <- log10(zval)
}
zmin <- zlim[[1]]
zmax <- zlim[[2]]

if (is.double(col)) {
pal_fun <- if (is.function(pal)) pal else colorRampPalette(pal)
batches <- pal_fun(steps_color)
Y <- seq(ymin, ymax, length.out = length(batches) + 1)
y <- seq(ymin, ymax, length.out = length(batches) + 1)
} else {
idx_c <- seq(min(zval_num), max(zval_num))
idx_c[!(idx_c %in% zval_num)] <- NA

batches <- pal[idx_c]
Y <- seq(ymin, ymax, length.out = length(idx_c) + 1)
y <- seq(ymin, ymax, length.out = length(idx_c) + 1)
}
rect(xmin, Y[-length(Y)], xmax, Y[-1], col = batches, border = NA)

rect(xmin, y[-length(y)], xmax, y[-1], col = batches, border = NA)
rect(xmin, ymin, xmax, ymax, border = col_lab)

dx <- xmax - xmin
dy <- ymax - ymin
if (left) {
Dx <- -dx
dx <- -dx
pos <- 2
xpos <- xmin + Dx * .5
}
else {
Dx <- +dx
xpos <- xmin + dx * .5
} else {
dx <- +dx
pos <- 4
xpos <- xmax + Dx * .5
xpos <- xmax + dx * .5
}

zval_txt <- if (is.double(col)) formatC(zval, digits = digit, format = 'fg') else zval
Ypos <- ymin + (zval_num - zmin)/(zmax - zmin) * dy

y_pos <- ymin + (zval_num - zmin) / (zmax - zmin) * dy
if (draw_ticks) {
if (is.double(col))
segments(xmax, Ypos, xpos + Dx * .25, Ypos, col = col_lab)
text(xpos, Ypos, zval_txt, pos = pos, col = col_lab, cex = cex_axis, ...)
segments(xmax, y_pos, xpos + dx * .25, y_pos, col = col_lab)
text(xpos, y_pos, zval_txt, pos = pos, col = col_lab, cex = cex_axis, ...)
}

if (!is.null(main)) {
for (i in length(main):1)
for (i in rev(seq_along(main)))
text(x = mean(c(xmin, xmax)),
y = ymax + .05 * (length(main) - i + 1),
labels = main[i],
Expand Down
Loading

0 comments on commit 9c60c3b

Please sign in to comment.