Skip to content

Commit

Permalink
Merge pull request #45 from invenia/gm/remove_mapslices
Browse files Browse the repository at this point in the history
Replace mapslices with selectdim
  • Loading branch information
Glenn Moynihan authored Mar 9, 2021
2 parents f6bb5e4 + 99efde3 commit 99b65ee
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FeatureTransforms"
uuid = "8fd68953-04b8-4117-ac19-158bf6de9782"
authors = ["Invenia Technical Computing Corporation"]
version = "0.2.0"
version = "0.2.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
3 changes: 1 addition & 2 deletions docs/src/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ For example, given a `Matrix`, `dims=1` slices the data column-wise and `inds=[2

!!! note

In general, the `dims` argument uses the convention of `mapslices`, which is called behind the scenes when applying transforms to slices of data.
In practice, this means that users can expect the `dims` keyword to behave exactly as `mean(A; dims=d)` would; the transformation will be applied to the elements along the dimension `d` and, for operations like `mean` or `sum`, reduce across this dimension.
In general, users can expect the `dims` keyword to behave exactly as `mean(A; dims=d)` would; the transformation will be applied to the elements along the dimension `d` and, for operations like `mean` or `sum`, reduce across this dimension.

```jldoctest transforms
julia> M
Expand Down
4 changes: 1 addition & 3 deletions src/transformers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ function apply(A::AbstractArray, t::Transform; dims=:, inds=:, kwargs...)
end
end

return @views mapslices(A, dims=dims) do x
_apply(x[inds], t; kwargs...)
end
return _apply(selectdim(A, dims, inds), t; kwargs...)
end

"""
Expand Down
24 changes: 8 additions & 16 deletions test/one_hot_encoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
@test FeatureTransforms.apply(x, ohe; inds=2:4) == [0 0 1; 0 1 0; 0 0 1]
@test FeatureTransforms.apply(x, ohe; dims=:) == expected

@test_throws DimensionMismatch FeatureTransforms.apply(x, ohe; dims=1)
@test_throws DimensionMismatch FeatureTransforms.apply(x, ohe; dims=1, inds=[2, 4])
@test FeatureTransforms.apply(x, ohe; dims=1) == expected
@test FeatureTransforms.apply(x, ohe; dims=1, inds=[2, 4]) == [0 0 1; 0 0 1]

@test_throws BoundsError FeatureTransforms.apply(x, ohe; dims=2)
end
Expand All @@ -51,10 +51,8 @@

@test FeatureTransforms.apply(M, ohe) == expected

@testset "dims" begin
@test FeatureTransforms.apply(M, ohe; dims=:) == expected
@test_throws DimensionMismatch FeatureTransforms.apply(M, ohe; dims=1)
@test_throws DimensionMismatch FeatureTransforms.apply(M, ohe; dims=2)
@testset "dims=:$d" for d in (1, 2, Colon())
@test FeatureTransforms.apply(M, ohe; dims=d) == expected
end

@testset "inds" begin
Expand All @@ -68,14 +66,11 @@
A = AxisArray(M, foo=["a", "b"], bar=["x", "y"])
expected = [1 0 0 0 0; 0 0 0 1 0; 0 1 0 0 0; 0 0 0 0 1]

@testset "dims" begin
transformed = FeatureTransforms.apply(A, ohe; dims=:)
@testset "dims = $d" for d in (1, 2, Colon())
transformed = FeatureTransforms.apply(A, ohe; dims=d)
# AxisArray doesn't preserve the type it operates on
@test transformed isa AbstractArray
@test transformed == expected

@test_throws DimensionMismatch FeatureTransforms.apply(A, ohe; dims=1)
@test_throws DimensionMismatch FeatureTransforms.apply(A, ohe; dims=2)
end

@testset "inds" begin
Expand All @@ -89,14 +84,11 @@
A = KeyedArray(M, foo=["a", "b"], bar=["x", "y"])
expected = [1 0 0 0 0; 0 0 0 1 0; 0 1 0 0 0; 0 0 0 0 1]

@testset "dims" begin
transformed = FeatureTransforms.apply(A, ohe; dims=:)
@testset "dims = $d" for d in (1, 2, Colon())
transformed = FeatureTransforms.apply(A, ohe; dims=d)
# This transform doesn't preserve the type it operates on
@test transformed isa AbstractArray
@test transformed == expected

@test_throws DimensionMismatch FeatureTransforms.apply(A, ohe; dims=1)
@test_throws DimensionMismatch FeatureTransforms.apply(A, ohe; dims=2)
end

@testset "inds" begin
Expand Down

2 comments on commit 99b65ee

@glennmoy
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

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

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.2.1 -m "<description of version>" 99b65ee845c7f53cab36d196ad33fc6206eff874
git push origin v0.2.1

Please sign in to comment.