Skip to content

Commit

Permalink
refactor: clean and fix custom checks and use set_values for param_set (
Browse files Browse the repository at this point in the history
#49)

* refactor: clean and fix custom checks and use set_values for param_set

* fix: fix positive count check

* refactor: change back to numeric

* fix: replace and with or in check_centers

* fix: fix showplot check for dbscanfpc and fix bracket for dbscan

* fix: encapsulate crate functions inside lambda function and update .lintr w/ minor styling changes

* refactor: add back newline between train and predict methods

* fix: don't wrap check_centers in lambda function

* chore: bump mlr3misc and paradox min version to 0.10 and document

* chore: add missing licence file

* chore: update precommit
  • Loading branch information
m-muecke authored Feb 6, 2024
1 parent d4c49fb commit 9186eb0
Show file tree
Hide file tree
Showing 32 changed files with 361 additions and 234 deletions.
6 changes: 3 additions & 3 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
linters: with_defaults(
linters: linters_with_defaults(
# lintr defaults: https://github.com/jimhester/lintr#available-linters
# the following setup changes/removes certain linters
assignment_linter = NULL, # do not force using <- for assignments
object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names
cyclocomp_linter = NULL, # do not check function complexity
commented_code_linter = NULL, # allow code in comments
line_length_linter = line_length_linter(120)
line_length_linter = line_length_linter(120),
object_length_linter = object_length_linter(40)
)

61 changes: 43 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
# All available hooks: https://pre-commit.com/hooks.html
# R specific hooks: https://github.com/lorenzwalthert/precommit
repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.1.2
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.0
hooks:
# any R project
- id: style-files
args: [--style_pkg=styler, --style_fun=mlr_style] # install with remotes::install_github("pat-s/styler@mlr-style")
# - id: lintr
- id: parsable-R
- id: no-browser-statement
- id: readme-rmd-rendered
# R package development
- id: roxygenize
- id: use-tidy-description
#- id: deps-in-desc
args: [--allow_private_imports]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
- id: style-files
args: [--style_pkg=styler.mlr, --style_fun=mlr_style]
additional_dependencies:
- mlr-org/styler.mlr
- id: roxygenize
additional_dependencies:
- backports
- checkmate
- clue
- fpc
- cluster
- data.table
- mlr3misc
- paradox
- R6
# codemeta must be above use-tidy-description when both are used
- id: use-tidy-description
- id: readme-rmd-rendered
- id: parsable-R
- id: no-browser-statement
- id: deps-in-desc
exclude: data-raw|inst
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
args: ['--maxkb=200']
- id: check-added-large-files
args: [--maxkb=200]
- id: file-contents-sorter
files: '^\.Rbuildignore$'
- id: end-of-file-fixer
exclude: '\.Rd'
- repo: local
hooks:
- id: forbid-to-commit
name: Don't commit common R artifacts
entry: Cannot commit .Rhistory, .RData, .Rds or .rds.
language: fail
files: '\.Rhistory|\.RData|\.Rds|\.rds$'
# `exclude: <regex>` to allow committing specific files.
ci:
autoupdate_schedule: monthly
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Imports:
fpc,
cluster,
data.table,
mlr3misc (>= 0.9.4),
paradox,
mlr3misc (>= 0.10.0),
paradox (>= 0.10.0),
R6,
stats
Suggests:
Expand All @@ -43,7 +43,7 @@ Suggests:
testthat (>= 3.0.0)
Encoding: UTF-8
Roxygen: list(markdown = TRUE, r6 = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Collate:
'LearnerClust.R'
'aaa.R'
Expand Down
165 changes: 165 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.


This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

0. Additional Definitions.

As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.

"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".

The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

1. Exception to Section 3 of the GNU GPL.

You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

2. Conveying Modified Versions.

If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or

b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.

3. Object Code Incorporating Material from Library Header Files.

The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the object code with a copy of the GNU GPL and this license
document.

4. Combined Works.

You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.

c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.

d) Do one of the following:

0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.

1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.

e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)

5. Combined Libraries.

You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.

b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.

6. Revised Versions of the GNU Lesser General Public License.

The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
5 changes: 2 additions & 3 deletions R/LearnerClust.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ LearnerClust = R6Class("LearnerClust",

#' @description
#' Creates a new instance of this [R6][R6::R6Class] class.
initialize = function(id, param_set = ps(), predict_types = "partition",
feature_types = character(), properties = character(),
packages = character(), label = NA_character_, man = NA_character_) {
initialize = function(id, param_set = ps(), predict_types = "partition", feature_types = character(),
properties = character(), packages = character(), label = NA_character_, man = NA_character_) {
super$initialize(
id = id, task_type = "clust", param_set = param_set,
predict_types = predict_types,
Expand Down
18 changes: 7 additions & 11 deletions R/LearnerClustAffinityPropagation.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ LearnerClustAP = R6Class("LearnerClustAP",
initialize = function() {
ps = ps(
s = p_uty(tags = c("required", "train")),
p = p_uty(custom_check = function(x) {
if (test_numeric(x)) {
return(TRUE)
} else {
stopf("`p` needs to be a numeric vector")
}
}, default = NA, tags = "train"),
q = p_dbl(lower = 0L, upper = 1, tags = "train"),
p = p_uty(default = NA, tags = "train", custom_check = crate(function(x) check_numeric(x))),
q = p_dbl(lower = 0, upper = 1, tags = "train"),
maxits = p_int(lower = 1L, default = 1000L, tags = "train"),
convits = p_int(lower = 1L, default = 100L, tags = "train"),
lam = p_dbl(lower = 0.5, upper = 1, default = 0.9, tags = "train"),
Expand Down Expand Up @@ -69,14 +63,16 @@ LearnerClustAP = R6Class("LearnerClustAP",

return(m)
},

.predict = function(task) {
sim_func = self$param_set$values$s
exemplar_data = attributes(self$model)$exemplar_data

d = task$data()
sim_mat = sim_func(rbind(exemplar_data, d),
sel = (1:nrow(d)) +
nrow(exemplar_data))[1:nrow(exemplar_data), ]
sim_mat = sim_func(
rbind(exemplar_data, d),
sel = (seq_len(nrow(d))) + nrow(exemplar_data)
)[seq_len(nrow(exemplar_data)), ]
partition = unname(apply(sim_mat, 2, which.max))
PredictionClust$new(task = task, partition = partition)
}
Expand Down
28 changes: 16 additions & 12 deletions R/LearnerClustAgnes.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,29 @@ LearnerClustAgnes = R6Class("LearnerClustAgnes",
ps = ps(
metric = p_fct(default = "euclidean", levels = c("euclidean", "manhattan"), tags = "train"),
stand = p_lgl(default = FALSE, tags = "train"),
method = p_fct(default = "average", levels = c("average", "single", "complete", "ward", "weighted", "flexible", "gaverage"), tags = "train"),
method = p_fct(
default = "average",
levels = c("average", "single", "complete", "ward", "weighted", "flexible", "gaverage"),
tags = "train"
),
trace.lev = p_int(lower = 0L, default = 0L, tags = "train"),
k = p_int(lower = 1L, default = 2L, tags = "predict"),
par.method = p_uty(tags = "train", custom_check = function(x) {
if (test_numeric(x) || test_list(x)) {
if (length(x) %in% c(1L, 3L, 4L)) {
return(TRUE)
}
stopf("`par.method` needs be of length 1, 3, or 4")
} else {
stopf("`par.method` needs to be a numeric vector")
}
})
par.method = p_uty(tags = "train", custom_check = crate(function(x) {
if (!(test_numeric(x) || test_list(x))) {
return("`par.method` needs to be a numeric vector")
}
if (length(x) %in% c(1L, 3L, 4L)) {
TRUE
} else {
"`par.method` needs be of length 1, 3, or 4"
}
}))
)

# param deps
ps$add_dep("par.method", "method", CondAnyOf$new(c("flexible", "gaverage")))

ps$values = list(k = 2L)
ps$set_values(k = 2L)

super$initialize(
id = "clust.agnes",
Expand Down
Loading

0 comments on commit 9186eb0

Please sign in to comment.