Skip to content

Commit

Permalink
add some skip_if_not_installed, fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppec committed Feb 24, 2025
1 parent 0f0d444 commit 2fe0d6a
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ output: github_document

<!-- badges: start -->

[![r-cmd-check](https://github.com/giuseppec/iml/actions/workflows/r.yml/badge.svg)](https://github.com/giuseppec/iml/actions)
[![r-cmd-check](https://github.com/giuseppec/iml/actions/workflows/r-cmd-check.yml/badge.svg)](https://github.com/giuseppec/iml/actions)
[![CRAN Status
Badge](https://www.r-pkg.org/badges/version-ago/iml)](https://CRAN.R-project.org/package=iml)
[![CRAN
Downloads](http://cranlogs.r-pkg.org/badges/grand-total/iml)](https://CRAN.R-project.org/package=iml)
Downloads](https://cranlogs.r-pkg.org/badges/grand-total/iml)](https://CRAN.R-project.org/package=iml)
[![codecov.io](https://codecov.io/github/christophM/iml/coverage.svg?branch=master)](https://codecov.io/github/christophM/iml?branch=master)
[![DOI](http://joss.theoj.org/papers/10.21105/joss.00786/status.svg)](https://doi.org/10.21105/joss.00786)
<!-- badges: end -->
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

<!-- badges: start -->

[![r-cmd-check](https://github.com/giuseppec/iml/actions/workflows/r.yml/badge.svg)](https://github.com/giuseppec/iml/actions)
[![r-cmd-check](https://github.com/giuseppec/iml/actions/workflows/r-cmd-check.yml/badge.svg)](https://github.com/giuseppec/iml/actions)
[![CRAN Status
Badge](https://www.r-pkg.org/badges/version-ago/iml)](https://CRAN.R-project.org/package=iml)
[![CRAN
Downloads](http://cranlogs.r-pkg.org/badges/grand-total/iml)](https://CRAN.R-project.org/package=iml)
Downloads](https://cranlogs.r-pkg.org/badges/grand-total/iml)](https://CRAN.R-project.org/package=iml)
[![codecov.io](https://codecov.io/github/christophM/iml/coverage.svg?branch=master)](https://codecov.io/github/christophM/iml?branch=master)
[![DOI](http://joss.theoj.org/papers/10.21105/joss.00786/status.svg)](https://doi.org/10.21105/joss.00786)
<!-- badges: end -->
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-FeatureEffect.R
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ test_that("method='ale' works for 2D numerical", {
})

test_that("iml::FeatureEffect with method='ale' equal to ALEPLot::ALEPlot", {
skip_if_not_installed("ALEPlot")
require("ALEPlot")

# one numerical feature
Expand Down
7 changes: 5 additions & 2 deletions tests/testthat/test-FeatureImp.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test_that("FeatureImp with difference", {
})

test_that("FeatureImp with 0 model error", {
skip_if_not_installed("mlr")
data(iris)
require("mlr")
lrn <- mlr::makeLearner("classif.rpart", predict.type = "prob")
Expand Down Expand Up @@ -110,8 +111,10 @@ test_that("Works for different repetitions.", {

test_that("Model receives data.frame without additional columns", {
# https://stackoverflow.com/questions/51980808/r-plotting-importance-feature-using-featureimpnew
library(mlr)
library(ranger)
skip_if_not_installed("mlr")
skip_if_not_installed("ranger")
require(mlr)
require(ranger)
data("iris")
tsk <- mlr::makeClassifTask(data = iris, target = "Species")
lrn <- mlr::makeLearner("classif.ranger", predict.type = "prob")
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-LocalModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ test_that("LocalModel works for single output and single feature", {
})

test_that("LocalModel works for multiple output", {
library("rpart")
skip_if_not_installed("rpart")
require("rpart")
clf <- rpart(Species ~ ., data = iris)
mod <- Predictor$new(clf, data = iris)
x.interest <- iris[1, -5]
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-MarginalGenerator.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# skip_if_not_installed("data.table")
library(data.table)
mg.dat <- data.table(
a = 1:10,
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-Predictor.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ test_that("equivalence", {
})

test_that("Missing predict.type for mlr gives warning", {
skip_if_not_installed("mlr")
task <- mlr::makeClassifTask(data = iris, target = "Species")
lrn <- mlr::makeLearner("classif.randomForest")
mod.mlr <- mlr::train(lrn, task)
Expand All @@ -166,12 +167,14 @@ test_that("Missing predict.type for mlr gives warning", {

# Test numeric predictions

skip_if_not_installed("mlr")
## mlr
task <- mlr::makeRegrTask(data = Boston, target = "medv")
lrn <- mlr::makeLearner("regr.rpart")
mod.mlr <- mlr::train(lrn, task)
predictor.mlr <- Predictor$new(mod.mlr, data = Boston)

skip_if_not_installed("mlr3")
# mlr3
task <- TaskRegr$new(id = "bn", backend = Boston, target = "medv")
learner <- lrn("regr.rpart")
Expand All @@ -182,6 +185,7 @@ predictor.mlr3 <- Predictor$new(learner, data = Boston)
mod.S3 <- mod.mlr$learner.model
predictor.S3 <- Predictor$new(mod.S3, data = Boston)

skip_if_not_installed("caret")
# caret
mod.caret <- caret::train(medv ~ .,
data = Boston, method = "knn",
Expand Down Expand Up @@ -266,6 +270,7 @@ test_that("Predictor errors with data, which includes NAs.", {
})

test_that("Predictor keeps factor names without X", {
skip_if_not_installed("rpart")
require(rpart)
dat <- data.frame(
y = factor(rep(c(1, 2), times = 5)),
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-create_predict_fun.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
skip_if_not_installed("mlr")
skip_if_not_installed("mlr3")
skip_if_not_installed("caret")

library("mlr")
library("mlr3")
library("caret")
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-find_y.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("find_y works with mlr", {
skip_if_not_installed("mlr")
require("mlr")
data(cars, package = "datasets")

Expand All @@ -10,6 +11,7 @@ test_that("find_y works with mlr", {
})

test_that("find_y works with caret", {
skip_if_not_installed("caret")
require("caret")
data(cars, package = "caret")

Expand All @@ -30,6 +32,7 @@ test_that("find_y works with lm", {


test_that("find_y works with rpart", {
skip_if_not_installed("rpart")
require("rpart")
data(cars, package = "datasets")

Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test-inferTask.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
skip_if_not_installed("randomForest")
library("randomForest")

rf <- randomForest(Species ~ ., data = iris, ntree = 1)
f <- function(x) predict(rf, newdata = x)

test_that("classificaton", {
skip_if_not_installed("mlr")
## use linear discriminant analysis to classify iris data
task <- mlr::makeClassifTask(data = iris, target = "Species")
learner <- mlr::makeLearner("classif.lda", method = "mle")
Expand All @@ -13,6 +15,7 @@ test_that("classificaton", {
})
expect_equal(tsk, "classification")

skip_if_not_installed("mlr3")
task <- mlr3::TaskClassif$new(id = "iris", backend = iris, target = "Species")
learner <- mlr3::lrn("classif.rpart")
mod <- learner$train(task)
Expand All @@ -21,7 +24,8 @@ test_that("classificaton", {

TrainData <- iris[, 1:4]
TrainClasses <- iris[, 5]


skip_if_not_installed("caret")
knnFit1 <- caret::train(TrainData, TrainClasses,
method = "knn",
preProcess = c("center", "scale"),
Expand All @@ -39,14 +43,14 @@ test_that("classificaton", {
})

test_that("regression", {

skip_if_not_installed("mlr")
## use linear discriminant analysis to classify iris data
task <- mlr::makeRegrTask(data = cars, target = "dist")
learner <- mlr::makeLearner("regr.lm")
mod <- mlr::train(learner, task)
expect_equal(inferTaskFromModel(mod), "regression")


skip_if_not_installed("caret")
lmFit <- caret::train(dist ~ .,
data = cars,
method = "lm"
Expand Down
11 changes: 0 additions & 11 deletions tic.R

This file was deleted.

0 comments on commit 2fe0d6a

Please sign in to comment.