Skip to content

Commit

Permalink
Merge pull request #409 from JuliaTrustworthyAI/408-get-coverage-up-t…
Browse files Browse the repository at this point in the history
…o-80

increase for data-processing
  • Loading branch information
pat-alt authored Mar 20, 2024
2 parents 94ceac1 + 774d6d3 commit bd98fcb
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 20 deletions.
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"

[weakdeps]
EvoTrees = "f6006082-12f8-11e9-0c9c-0d5d367ab1e5"
Expand All @@ -38,10 +37,9 @@ EvoTreesExt = "EvoTrees"
LaplaceReduxExt = "LaplaceRedux"

[compat]
CUDA = "3, 4, 5"
CategoricalArrays = "0.10"
ChainRulesCore = "1.15"
CUDA = "3, 4, 5"
cuDNN = "1"
DataFrames = "1"
DecisionTree = "0.12.3"
Distributions = "0.25.97"
Expand Down
2 changes: 1 addition & 1 deletion docs/setup_docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ setup_docs = quote
ENV["DATADEPS_ALWAYS_ACCEPT"] = "true"

# Counteractual data and model:
counterfactual_data = load_linearly_separable()
counterfactual_data = CounterfactualData(load_linearly_separable()...)
M = fit_model(counterfactual_data, :Linear)
target = 2
factual = 1
Expand Down
10 changes: 8 additions & 2 deletions src/data_preprocessing/counterfactual_data.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using MLJBase

"""
CounterfactualData(
X::AbstractMatrix, y::AbstractMatrix;
Expand Down Expand Up @@ -181,10 +183,14 @@ Outer constructor method that accepts a `Tables.MatrixTable`. By default, the in
"""
function CounterfactualData(X::Tables.MatrixTable, y::RawOutputArrayType; kwrgs...)
features_categorical = findall([scitype(x) <: AbstractVector{<:Finite} for x in X])
features_categorical = findall([
MLJBase.scitype(x) <: AbstractVector{<:Finite} for x in X
])
features_categorical =
length(features_categorical) == 0 ? nothing : features_categorical
features_continuous = findall([scitype(x) <: AbstractVector{<:Continuous} for x in X])
features_continuous = findall([
MLJBase.scitype(x) <: AbstractVector{<:Continuous} for x in X
])
features_continuous = length(features_continuous) == 0 ? nothing : features_continuous
X = permutedims(Tables.matrix(X))

Expand Down
9 changes: 5 additions & 4 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
CompatHelperLocal = "5224ae11-6099-4aaa-941d-3aab004bd678"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand All @@ -7,16 +8,16 @@ Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
LaplaceRedux = "c52c1a26-f7c5-402b-80be-ba1e638ad478"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
MLJDecisionTreeInterface = "c6f25543-311c-4c74-83dc-3ea6d1015661"
MLJModels = "d491faf4-2d78-11e9-2867-c94bc002c0b7"
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TaijaData = "9d524318-b4e6-4a65-86d2-b2b72d07866c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"

[compat]
Aqua = "0.8"
Aqua = "0.8"
10 changes: 10 additions & 0 deletions test/data/data_preprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ ENV["DATADEPS_ALWAYS_ACCEPT"] = "true"
counterfactual_data = DataPreprocessing.CounterfactualData(X, y; domain=(0, 0))
@test unique(DataPreprocessing.apply_domain_constraints(counterfactual_data, x))[1] == 0
end

@testset "Other" begin
# MatrixTable:
dataset = Iris()
X = dataset.features
y = dataset.targets
X = MLJBase.table(Tables.matrix(X))
y = y[:, 1]
counterfactual_data = CounterfactualData(X, y)
end
50 changes: 50 additions & 0 deletions test/generators/categorical_data.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using MLJModels: OneHotEncoder
using StatsBase: sample, Weights

@testset "Categorical data" begin

# Set up a simple categorical dataset:
N = 1000
X, ys = MLJBase.make_blobs(
N, 2; centers=2, as_table=false, center_box=(-5 => 5), cluster_std=0.5
)
ys .= ys .== 2
cat_values = ["X", "Y", "Z"]
xcat = map(ys) do y
if y == 1
x = sample(cat_values, Weights([0.8, 0.1, 0.1]))
else
x = sample(cat_values, Weights([0.1, 0.1, 0.8]))
end
end
xcat = categorical(xcat)
X = (x1=X[:, 1], x2=X[:, 2], x3=xcat)

# Encode the categorical feature:
hot = OneHotEncoder()
mach = MLJBase.fit!(machine(hot, X))
W = MLJBase.transform(mach, X)
X = permutedims(MLJBase.matrix(W))

# Create a counterfactual data object:
features_categorical = [collect(3:size(X, 1))]
counterfactual_data = CounterfactualData(
X, ys'; features_categorical=features_categorical
)

# Fit a model:
M = fit_model(counterfactual_data, :Linear)

# Sample a factual:
target = 1
factual = 0
chosen = rand(findall(predict_label(M, counterfactual_data) .== factual))
x = select_factual(counterfactual_data, chosen)

# Generate a counterfactual:
generator = GenericGenerator()
ce = generate_counterfactual(x, target, counterfactual_data, M, generator)

# Test:
@test true
end
1 change: 1 addition & 0 deletions test/generators/generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include("feature_tweak.jl")
include("counterfactuals.jl")
include("probe.jl")
include("growing_spheres.jl")
include("categorical_data.jl")

# generic tests
@testset "Construction" begin
Expand Down
27 changes: 27 additions & 0 deletions test/other/objectives.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CounterfactualExplanations
using CounterfactualExplanations.Models: fit_model
using CounterfactualExplanations.Objectives

# Counteractual data and model:
counterfactual_data = CounterfactualData(TaijaData.load_linearly_separable()...)
M = fit_model(counterfactual_data, :Linear)
target = 2
factual = 1
chosen = rand(findall(predict_label(M, counterfactual_data) .== factual))
x = select_factual(counterfactual_data, chosen)

# Search:
generator = GenericGenerator()
ce = generate_counterfactual(x, target, counterfactual_data, M, generator)

@testset "Losses" begin
for (lname, lfun) in Objectives.losses_catalogue
@test lfun(ce) isa AbstractFloat
end
end

@testset "Penalties" begin
for (pname, pfun) in Objectives.penalties_catalogue
@test pfun(ce) isa AbstractFloat
end
end
3 changes: 3 additions & 0 deletions test/other/other.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CounterfactualExplanations.reset!(flux_training_params)

@test true
6 changes: 6 additions & 0 deletions test/parallelization/parallelization.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
using CounterfactualExplanations

@test parallelizable(generate_counterfactual) == true
@test parallelizable(CounterfactualExplanations.Evaluation.evaluate) == true
@test parallelizable(predict_label) == false

@testset "Threads" begin
include("threads.jl")
end
3 changes: 3 additions & 0 deletions test/parallelization/threads.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CounterfactualExplanations
using CounterfactualExplanations.Evaluation: evaluate
using CounterfactualExplanations.Parallelization

counterfactual_data = synthetic[:classification_binary][:data]
Expand All @@ -14,4 +15,6 @@ ces = @with_parallelizer parallelizer begin
generate_counterfactual(xs, target, counterfactual_data, M, generator)
end

@with_parallelizer parallelizer evaluate(ces)

@test true
28 changes: 18 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using Chain: @chain
using CounterfactualExplanations
using CounterfactualExplanations.Convergence
using CounterfactualExplanations.DataPreprocessing
using CounterfactualExplanations.Evaluation
using CounterfactualExplanations.Generators
using CounterfactualExplanations.Models
using Test
using DataFrames
using EvoTrees
using Flux
using LinearAlgebra
using MLUtils
using Random
using Plots
using LaplaceRedux
using EvoTrees
using LinearAlgebra
using MLDatasets
using MLJBase
using MLJDecisionTreeInterface
using Printf
using CounterfactualExplanations.Convergence
using MLUtils
using Random
using TaijaData
using CounterfactualExplanations.Generators
using CounterfactualExplanations.Models
using CounterfactualExplanations.Evaluation
using CounterfactualExplanations.DataPreprocessing

Random.seed!(0)

Expand Down Expand Up @@ -52,7 +52,15 @@ generators = Generators.generator_catalogue
include("other/evaluation.jl")
end

@testset "Objectives" begin
include("other/objectives.jl")
end

@testset "Parallelization" begin
include("parallelization/parallelization.jl")
end

@testset "Other" begin
include("other/other.jl")
end
end

2 comments on commit bd98fcb

@pat-alt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103281

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.32 -m "<description of version>" bd98fcbb2ccb51505f1151a0f2f6de0bdd3ef9c6
git push origin v0.1.32

Please sign in to comment.