Skip to content

Commit

Permalink
Merge pull request #28 from JuliaTrustworthyAI/subsampling-needs-to-b…
Browse files Browse the repository at this point in the history
…e-seeded

Subsampling needs to be seeded
  • Loading branch information
pat-alt authored Jan 10, 2025
2 parents d6dc1d3 + 5cb6a3a commit 2da3f52
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 48 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

*Note*: We try to adhere to these practices as of version [v1.0.1].

## Version [1.0.1] - 2025-01-09
## Version [1.1.1] - 2025-01-10

### Changed

- Improved seeding behaviour for tabular and vision datasets.

## Version [1.1.0] - 2025-01-09

### Changed

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaijaData"
uuid = "9d524318-b4e6-4a65-86d2-b2b72d07866c"
authors = ["Patrick Altmeyer"]
version = "1.1.0"
version = "1.1.1"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
6 changes: 2 additions & 4 deletions src/synthetic/blobs.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""
load_blobs(n=250; seed=Random.GLOBAL_RNG, kwrgs...)
load_blobs(n=250; seed=data_seed, k=2, centers=2, kwrgs...)
Loads overlapping synthetic data.
"""
function load_blobs(n=250; seed=data_seed, k=2, centers=2, kwrgs...)
if isa(seed, Int)
seed = Xoshiro(seed)
end
seed = get_rng(seed)

X, y = MLJBase.make_blobs(n, k; centers=centers, rng=seed, kwrgs...)
X = permutedims(MLJBase.matrix(X))
Expand Down
11 changes: 6 additions & 5 deletions src/tabular/adult.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_uci_adult(n::Union{Nothing, Int}=1000)
load_uci_adult(n::Union{Nothing,Int}=1000; seed=data_seed)
Loads data from the UCI 'Adult' dataset.
"""
function load_uci_adult(n::Union{Nothing,Int}=1000)
function load_uci_adult(n::Union{Nothing,Int}=1000; seed=data_seed)
# Throw an exception if n < 1:
if !isnothing(n) && n < 1
throw(ArgumentError("n must be >= 1"))
Expand Down Expand Up @@ -44,9 +44,10 @@ function load_uci_adult(n::Union{Nothing,Int}=1000)

y = df.target

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
11 changes: 6 additions & 5 deletions src/tabular/california_housing.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_california_housing(n::Union{Nothing,Int}=5000)
load_california_housing(n::Union{Nothing,Int}=5000; seed=data_seed)
Loads California Housing data.
"""
function load_california_housing(n::Union{Nothing,Int}=5000)
function load_california_housing(n::Union{Nothing,Int}=5000; seed=data_seed)

# check that n is > 0
if !isnothing(n) && n <= 0
Expand All @@ -22,9 +22,10 @@ function load_california_housing(n::Union{Nothing,Int}=5000)
# Counterfactual data:
y = Int.(df.target)

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
11 changes: 6 additions & 5 deletions src/tabular/credit_default.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_credit_default(n::Union{Nothing,Int}=5000)
load_credit_default(n::Union{Nothing,Int}=5000; seed=data_seed)
Loads UCI Credit Default data.
"""
function load_credit_default(n::Union{Nothing,Int}=5000)
function load_credit_default(n::Union{Nothing,Int}=5000; seed=data_seed)

# Load:
df = CSV.read(joinpath(data_dir, "credit_default.csv"), DataFrames.DataFrame)
Expand All @@ -28,9 +28,10 @@ function load_credit_default(n::Union{Nothing,Int}=5000)
# X, y; features_categorical=features_categorical
# )

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
11 changes: 6 additions & 5 deletions src/tabular/german_credit.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_german_credit(n::Union{Nothing, Int}=nothing)
load_german_credit(n::Union{Nothing,Int}=nothing; seed=data_seed)
Loads UCI German Credit data.
"""
function load_german_credit(n::Union{Nothing,Int}=nothing)
function load_german_credit(n::Union{Nothing,Int}=nothing; seed=data_seed)
# Throw an exception if n > 1000:
if !isnothing(n) && n > 1000
throw(ArgumentError("n must be <= 1000"))
Expand All @@ -27,9 +27,10 @@ function load_german_credit(n::Union{Nothing,Int}=nothing)
# Counterfactual data:
y = convert(Vector, df.target)

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
11 changes: 6 additions & 5 deletions src/tabular/gmsc.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_gmsc(n::Union{Nothing,Int}=5000)
load_gmsc(n::Union{Nothing,Int}=5000; seed=data_seed)
Loads Give Me Some Credit (GMSC) data.
"""
function load_gmsc(n::Union{Nothing,Int}=5000)
function load_gmsc(n::Union{Nothing,Int}=5000; seed=data_seed)

# Load:
df = CSV.read(joinpath(data_dir, "gmsc.csv"), DataFrames.DataFrame)
Expand All @@ -18,9 +18,10 @@ function load_gmsc(n::Union{Nothing,Int}=5000)
# Counterfactual data:
y = df.target

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
18 changes: 16 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function subsample(X::AbstractMatrix, y::AbstractVector, n::Int)
using Random

"""
get_rng(seed::Union{Int,AbstractRNG})
Returns a random number generator based on the provided seed, if seed is an integer, or returns the seed itself if it's already an `AbstractRNG`.
"""
function get_rng(seed::Union{Int,AbstractRNG})
if isa(seed, Int)
seed = Xoshiro(seed)
end
return seed
end

function subsample(rng::AbstractRNG, X::AbstractMatrix, y::AbstractVector, n::Int)
# Get the unique classes in `y`.
classes_ = unique(y)

Expand All @@ -13,7 +27,7 @@ function subsample(X::AbstractMatrix, y::AbstractVector, n::Int)
reduce(
vcat,
[
StatsBase.sample(findall(y .== cls), n_per_class; replace=true) for
StatsBase.sample(rng, findall(y .== cls), n_per_class; replace=true) for
cls in classes_
],
),
Expand Down
11 changes: 6 additions & 5 deletions src/vision/cifar_10.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_cifar_10(n::Union{Nothing, Int}=nothing)
load_cifar_10(n::Union{Nothing, Int}=nothing; seed=data_seed)
Loads data from the CIFAR-10 dataset.
"""
function load_cifar_10(n::Union{Nothing,Int}=nothing)
function load_cifar_10(n::Union{Nothing,Int}=nothing; seed=data_seed)
X, y = MLDatasets.CIFAR10()[:] # [:] gives us X, y
X = Flux.flatten(X)
X = X .* 2 .- 1 # normalization between [-1, 1]
Expand All @@ -13,9 +13,10 @@ function load_cifar_10(n::Union{Nothing,Int}=nothing)
# X, y; domain=(-1.0, 1.0), standardize=false
# )

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
11 changes: 6 additions & 5 deletions src/vision/fashion_mnist.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_fashion_mnist(n::Union{Nothing,Int}=nothing)
load_fashion_mnist(n::Union{Nothing,Int}=nothing; seed=data_seed)
Loads FashionMNIST data.
"""
function load_fashion_mnist(n::Union{Nothing,Int}=nothing)
function load_fashion_mnist(n::Union{Nothing,Int}=nothing; seed=data_seed)
X, y = MLDatasets.FashionMNIST(:train)[:]
X = Flux.flatten(X)
X = X .* 2.0f0 .- 1.0f0
Expand All @@ -13,9 +13,10 @@ function load_fashion_mnist(n::Union{Nothing,Int}=nothing)
# X, y; domain=(-1.0, 1.0), standardize=false
# )

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down
11 changes: 6 additions & 5 deletions src/vision/mnist.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
load_mnist(n::Union{Nothing,Int}=nothing)
load_mnist(n::Union{Nothing,Int}=nothing; seed=data_seed)
Loads MNIST data.
"""
function load_mnist(n::Union{Nothing,Int}=nothing)
function load_mnist(n::Union{Nothing,Int}=nothing; seed=data_seed)
X, y = MLDatasets.MNIST(:train)[:]
X = Flux.flatten(X)
X = X .* 2.0f0 .- 1.0f0
Expand All @@ -13,9 +13,10 @@ function load_mnist(n::Union{Nothing,Int}=nothing)
# X, y; domain=(-1.0, 1.0), standardize=false
# )

# Undersample:
if !isnothing(n)
X, y = subsample(X, y, n)
# Randomly under-/over-sample:
rng = get_rng(seed)
if !isnothing(n) && n != size(X)[2]
X, y = subsample(rng, X, y, n)
end

return (X, y)
Expand Down

2 comments on commit 2da3f52

@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/122738

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 v1.1.1 -m "<description of version>" 2da3f5242f027ad33333df2a75be3be66b3939f3
git push origin v1.1.1

Please sign in to comment.