Skip to content

chore: use air formatting #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
^Meta$
^README\.Rmd$
^README\.html$
^[\.]?air\.toml$
^\.Renviron$
^\.Rproj\.user$
^\.ccache$
Expand Down
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.3.9008
hooks:
- id: style-files
args: [--style_pkg=styler.mlr, --style_fun=mlr_style]
additional_dependencies:
- mlr-org/styler.mlr
- id: roxygenize
additional_dependencies:
- R6
Expand Down
3 changes: 2 additions & 1 deletion R/PipeOpFDACor.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#' po_cor = po("fda.cor")
#' task_cor = po_cor$train(list(task))[[1L]]
#' task_cor
PipeOpFDACor = R6Class("PipeOpFDACor",
PipeOpFDACor = R6Class(
"PipeOpFDACor",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
Expand Down
71 changes: 38 additions & 33 deletions R/PipeOpFDAExtract.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
#' )
#' task_custom = po_custom$train(list(task))[[1L]]
#' task_custom
PipeOpFDAExtract = R6Class("PipeOpFDAExtract",
PipeOpFDAExtract = R6Class(
"PipeOpFDAExtract",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
Expand All @@ -63,41 +64,44 @@ PipeOpFDAExtract = R6Class("PipeOpFDAExtract",
drop = p_lgl(tags = c("train", "predict", "required")),
left = p_dbl(tags = c("train", "predict", "required")),
right = p_dbl(tags = c("train", "predict", "required")),
features = p_uty(tags = c("train", "predict", "required"), custom_check = crate(function(x) {
if (test_character(x)) {
return(check_subset(x, choices = c("mean", "median", "min", "max", "slope", "var")))
}
if (test_list(x)) {
res = check_list(x, types = c("character", "function"), any.missing = FALSE, unique = TRUE)
if (!isTRUE(res)) {
return(res)
features = p_uty(
tags = c("train", "predict", "required"),
custom_check = crate(function(x) {
if (test_character(x)) {
return(check_subset(x, choices = c("mean", "median", "min", "max", "slope", "var")))
}
nms = names2(x)
res = check_names(nms[!is.na(nms)], "unique")
if (!isTRUE(res)) {
return(res)
}
for (i in seq_along(x)) {
if (is.function(x[[i]])) {
res = check_function(x[[i]], args = c("arg", "value"))
if (!isTRUE(res)) {
return(res)
}
res = check_names(nms[i])
if (!isTRUE(res)) {
return(res)
}
} else {
res = check_choice(x[[i]], choices = c("mean", "median", "min", "max", "slope", "var"))
if (!isTRUE(res)) {
return(res)
if (test_list(x)) {
res = check_list(x, types = c("character", "function"), any.missing = FALSE, unique = TRUE)
if (!isTRUE(res)) {
return(res)
}
nms = names2(x)
res = check_names(nms[!is.na(nms)], "unique")
if (!isTRUE(res)) {
return(res)
}
for (i in seq_along(x)) {
if (is.function(x[[i]])) {
res = check_function(x[[i]], args = c("arg", "value"))
if (!isTRUE(res)) {
return(res)
}
res = check_names(nms[i])
if (!isTRUE(res)) {
return(res)
}
} else {
res = check_choice(x[[i]], choices = c("mean", "median", "min", "max", "slope", "var"))
if (!isTRUE(res)) {
return(res)
}
}
}
return(TRUE)
}
return(TRUE)
}
"Features must be a character or list"
}))
"Features must be a character or list"
})
)
)
param_set$set_values(
drop = TRUE,
Expand Down Expand Up @@ -144,7 +148,8 @@ PipeOpFDAExtract = R6Class("PipeOpFDAExtract",
if (is.function(feature)) {
return(feature)
}
switch(feature,
switch(
feature,
mean = fmean,
median = fmedian,
min = fmin,
Expand Down
3 changes: 2 additions & 1 deletion R/PipeOpFDAFlatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#' task = tsk("fuel")
#' pop = po("fda.flatten")
#' task_flat = pop$train(list(task))
PipeOpFDAFlatten = R6Class("PipeOpFDAFlatten",
PipeOpFDAFlatten = R6Class(
"PipeOpFDAFlatten",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
Expand Down
28 changes: 17 additions & 11 deletions R/PipeOpFDAInterpol.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
#' pop = po("fda.interpol")
#' task_interpol = pop$train(list(task))[[1L]]
#' task_interpol$data()
PipeOpFDAInterpol = R6Class("PipeOpFDAInterpol",
PipeOpFDAInterpol = R6Class(
"PipeOpFDAInterpol",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
Expand All @@ -56,17 +57,22 @@ PipeOpFDAInterpol = R6Class("PipeOpFDAInterpol",
#' otherwise be set during construction. Default `list()`.
initialize = function(id = "fda.interpol", param_vals = list()) {
param_set = ps(
grid = p_uty(tags = c("train", "predict", "required"), custom_check = crate(function(x) {
if (test_string(x)) {
return(check_choice(x, choices = c("union", "intersect", "minmax")))
}
if (test_numeric(x, any.missing = FALSE, min.len = 1L)) {
return(TRUE)
}
"Must be either a string or numeric vector"
})),
grid = p_uty(
tags = c("train", "predict", "required"),
custom_check = crate(function(x) {
if (test_string(x)) {
return(check_choice(x, choices = c("union", "intersect", "minmax")))
}
if (test_numeric(x, any.missing = FALSE, min.len = 1L)) {
return(TRUE)
}
"Must be either a string or numeric vector"
})
),
method = p_fct(
c("linear", "spline", "fill_extend", "locf", "nocb"), default = "linear", tags = c("train", "predict")
c("linear", "spline", "fill_extend", "locf", "nocb"),
default = "linear",
tags = c("train", "predict")
),
left = p_dbl(tags = c("train", "predict")),
right = p_dbl(tags = c("train", "predict"))
Expand Down
3 changes: 2 additions & 1 deletion R/PipeOpFDAScaleRange.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#' po_scale = po("fda.scalerange", lower = -1, upper = 1)
#' task_scale = po_scale$train(list(task))[[1L]]
#' task_scale$data()
PipeOpFDAScaleRange = R6Class("PipeOpFDAScaleRange",
PipeOpFDAScaleRange = R6Class(
"PipeOpFDAScaleRange",
inherit = PipeOpTaskPreproc,
public = list(
#' @description Initializes a new instance of this Class.
Expand Down
15 changes: 11 additions & 4 deletions R/PipeOpFDASmooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#' task_smooth = po_smooth$train(list(task))[[1L]]
#' task_smooth
#' task_smooth$data(cols = c("NIR", "UVVIS"))
PipeOpFDASmooth = R6Class("PipeOpFDASmooth",
PipeOpFDASmooth = R6Class(
"PipeOpFDASmooth",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
Expand All @@ -42,9 +43,14 @@ PipeOpFDASmooth = R6Class("PipeOpFDASmooth",
#' otherwise be set during construction. Default `list()`.
initialize = function(id = "fda.smooth", param_vals = list()) {
param_set = ps(
method = p_fct(default = "lowess", c("lowess", "rollmean", "rollmedian", "savgol"), tags = c("train", "predict")), # nolint
method = p_fct(
default = "lowess",
c("lowess", "rollmean", "rollmedian", "savgol"),
tags = c("train", "predict")
),
args = p_uty(
tags = c("train", "predict", "required"), custom_check = crate(function(x) check_list(x, names = "unique"))
tags = c("train", "predict", "required"),
custom_check = crate(function(x) check_list(x, names = "unique"))
),
verbose = p_lgl(tags = c("train", "predict", "required"))
)
Expand Down Expand Up @@ -72,7 +78,8 @@ PipeOpFDASmooth = R6Class("PipeOpFDASmooth",
return(dt)
}
for (j in seq_along(dt)) {
set(dt,
set(
dt,
j = j,
value = suppressMessages(invoke(tf::tf_smooth, x = dt[[j]], method = pars$method, .args = pars$args))
)
Expand Down
3 changes: 2 additions & 1 deletion R/PipeOpFDATsfeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
#' po_tsfeats = po("fda.tsfeats")
#' task_tsfeats = po_tsfeats$train(list(task))[[1L]]
#' task_tsfeats$data()
PipeOpFDATsfeatures = R6Class("PipeOpFDATsfeatures",
PipeOpFDATsfeatures = R6Class(
"PipeOpFDATsfeatures",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
Expand Down
3 changes: 2 additions & 1 deletion R/PipeOpFPCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#' po_fpca = po("fda.fpca", n_components = 3L)
#' task_fpca = po_fpca$train(list(task))[[1L]]
#' task_fpca$data()
PipeOpFPCA = R6Class("PipeOpFPCA",
PipeOpFPCA = R6Class(
"PipeOpFPCA",
inherit = PipeOpTaskPreproc,
public = list(
#' @description Initializes a new instance of this Class.
Expand Down
9 changes: 6 additions & 3 deletions R/bibentries.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' @importFrom utils bibentry
# nolint start
bibentries = c(
ferraty2003curves = bibentry("article",
ferraty2003curves = bibentry(
"article",
title = "Curves discrimination: a nonparametric functional approach",
author = "Ferraty, Fr\xc3\xa9dric and Vieu, Philippe",
journal = "Computational Statistics & Data Analysis",
Expand All @@ -11,7 +12,8 @@ bibentries = c(
year = "2003",
publisher = "Elsevier"
),
brockhaus2015functional = bibentry("article",
brockhaus2015functional = bibentry(
"article",
title = "The functional linear array model",
author = "Brockhaus, Sarah and Scheipl, Fabian and Hothorn, Torsten and Greven, Sonja",
journal = "Statistical Modelling",
Expand All @@ -21,7 +23,8 @@ bibentries = c(
year = "2015",
publisher = "SAGE Publications Sage India: New Delhi, India"
),
goldsmith2011penalized = bibentry("article",
goldsmith2011penalized = bibentry(
"article",
title = "Penalized functional regression",
author = "Goldsmith, Jeff and Bobb, Jennifer and Crainiceanu, Ciprian M and Caffo, Brian and Reich, Daniel",
journal = "Journal of Computational and Graphical Statistics",
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ register_mlr3pipelines = function() {
register_namespace_callback(pkgname, "mlr3pipelines", register_mlr3pipelines)
}

.onUnload = function(libPaths) { # nolint
.onUnload = function(libPaths) {
walk(names(mlr3fda_tasks), function(nm) mlr_tasks$remove(nm))
walk(names(mlr3fda_pipeops), function(nm) mlr_pipeops$remove(nm))
mlr_reflections$task_feature_types =
Expand Down
2 changes: 2 additions & 0 deletions air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[format]
line-width = 120
44 changes: 24 additions & 20 deletions inst/bench/bench.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ library(mlr3pipelines)
set.seed(1234)
setDTthreads(threads = 1)

generate_data = function(n_patients = 100,
n_weeks = 10,
type = c("reg", "irreg")) {
generate_data = function(n_patients = 100, n_weeks = 10, type = c("reg", "irreg")) {
type = match.arg(type)
if (type == "reg") {
week = 1:n_weeks
Expand Down Expand Up @@ -52,27 +50,29 @@ analyse_dplyr = function(patients, window_start, window_end, choice) {

analyse_dt = function(patients, window_start, window_end, choice) {
if (choice == "one") {
patients[between(week, window_start, window_end), .(
mean = mean(measurement_value)
), keyby = .(patient_id, measurement_type)]
patients[
between(week, window_start, window_end),
.(
mean = mean(measurement_value)
),
keyby = .(patient_id, measurement_type)
]
} else {
patients[between(week, window_start, window_end), .(
mean = mean(measurement_value),
var = var(measurement_value),
slope = coef(lm(measurement_value ~ week))[[2]]
), keyby = .(patient_id, measurement_type)]
patients[
between(week, window_start, window_end),
.(
mean = mean(measurement_value),
var = var(measurement_value),
slope = coef(lm(measurement_value ~ week))[[2]]
),
keyby = .(patient_id, measurement_type)
]
}
}

build_graph = function(left, right, choice) {
features = switch(choice, one = "mean", three = c("mean", "var", "slope"))
po("ffs",
features = features,
id = "features",
drop = FALSE,
left = left,
right = right
)
po("ffs", features = features, id = "features", drop = FALSE, left = left, right = right)
}

analyse_fda = function(graph, task) {
Expand All @@ -88,7 +88,9 @@ results_one = bench::press(
window_end = floor(n_weeks * 0.8)

patients = generate_data(
n_patients = n_patients, n_weeks = n_weeks, type = type
n_patients = n_patients,
n_weeks = n_weeks,
type = type
)
patients_long = patients |>
tidyr::pivot_longer(
Expand Down Expand Up @@ -129,7 +131,9 @@ results_three = bench::press(
window_end = floor(n_weeks * 0.8)

patients = generate_data(
n_patients = n_patients, n_weeks = n_weeks, type = type
n_patients = n_patients,
n_weeks = n_weeks,
type = type
)
patients_long = patients |>
tidyr::pivot_longer(
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test_PipeOpFDAExtract.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ test_that("PipeOpFDAExtract works", {
pop = po("fda.extract", features = c("mean", "median", "min"), drop = TRUE, left = 100, right = 200)
task_pop = train_pipeop(pop, list(task))[[1L]]
expected = data.table(
y = 1:2, f_mean = rep(NA_real_, 2L), f_median = rep(NA_real_, 2L), f_min = rep(NA_real_, 2L)
y = 1:2,
f_mean = rep(NA_real_, 2L),
f_median = rep(NA_real_, 2L),
f_min = rep(NA_real_, 2L)
)
expect_identical(task_pop$data(), expected)

Expand Down Expand Up @@ -184,9 +187,7 @@ test_that("ffind works", {
expect_identical(ffind(1:10, 5, 15), c(5L, 10L))

expect_identical(
ffind(c(-3876, -3798, -3453, -3363, -2974, -2953, -2871, -1917, -1335, -1304, -725, 10),
left = -200, right = 0
),
ffind(c(-3876, -3798, -3453, -3363, -2974, -2953, -2871, -1917, -1335, -1304, -725, 10), left = -200, right = 0),
rep(NA_integer_, 2L)
)
})
Loading