-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Glenn Moynihan
authored
Apr 16, 2021
1 parent
e95ea9c
commit eb9daee
Showing
11 changed files
with
169 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# [TestUtils](@id test-utils) | ||
|
||
`FeatureTransforms.TestUtils` is used to test new data types that wish to support the [transform interface](@ref transform-interface) described in the documentation. | ||
It provides various test fakes and utilities to help with doing so. | ||
|
||
## API | ||
|
||
```@autodocs | ||
Modules=[FeatureTransforms.TestUtils] | ||
Order=[:module, :type, :function] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
FeatureTransforms.TestUtils | ||
Provides fake [`Transform`](@ref)s and utilities for testing purposes only. | ||
Each fake [`Transform`](@ref) has different a different `cardinality`: `OneToOne`, OneToMany`, | ||
`ManyToOne`, or `ManyToMany`. So when users extend FeatureTransforms.jl for new data types | ||
they only need to test against these 4 fakes to guarantee their type can support any | ||
[`Transform`](@ref) in the package. | ||
Similarly, `is_transformable` is used to check that the output of a `transform` pipeline is | ||
a transformable type. | ||
""" | ||
|
||
module TestUtils | ||
|
||
using ..FeatureTransforms | ||
using ..FeatureTransforms: OneToOne, OneToMany, ManyToOne, ManyToMany | ||
using Tables | ||
|
||
export FakeOneToOneTransform, FakeOneToManyTransform | ||
export FakeManyToOneTransform, FakeManyToManyTransform | ||
export is_transformable | ||
|
||
for C in (:OneToOne, :OneToMany, :ManyToOne, :ManyToMany) | ||
FT = Symbol(:Fake, C, :Transform) | ||
@eval begin | ||
""" | ||
$($FT) <: Transform | ||
A fake `$($C)` transform for test purposes. Calling `apply` will return an | ||
array of ones with a size and dimension matching the `cardinality` of the transform. | ||
""" | ||
struct $FT <: Transform end | ||
FeatureTransforms.cardinality(::$FT) = $C() | ||
end | ||
end | ||
|
||
function FeatureTransforms._apply(A, ::FakeOneToOneTransform; kwargs...) | ||
return ones(size(A)) | ||
end | ||
|
||
function FeatureTransforms._apply(A, ::FakeOneToManyTransform; kwargs...) | ||
return hcat(ones(size(A)), ones(size(A))) | ||
end | ||
|
||
function FeatureTransforms._apply(A, ::FakeManyToOneTransform; dims, kwargs...) | ||
return ones(size(first(A))) | ||
end | ||
|
||
function FeatureTransforms._apply(A, ::FakeManyToManyTransform; kwargs...) | ||
return hcat(ones(size(A)), ones(size(A))) | ||
end | ||
|
||
""" | ||
is_transformable(x) | ||
Determine if `x` is both a valid input and output of any [`Transform`](@ref), i.e. that it | ||
follows the [`transform`](@ref) interface. | ||
Currently, all subtypes of `Table`s and `AbstractArray`s are transformable. | ||
""" | ||
is_transformable(::AbstractArray) = true | ||
is_transformable(x) = Tables.istable(x) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using FeatureTransforms.TestUtils | ||
|
||
@testset "test_utils.jl" begin | ||
|
||
@testset "FakeOneToOneTransform" begin | ||
t = FakeOneToOneTransform() | ||
@test cardinality(t) == OneToOne() | ||
|
||
x = [1, 2, 3] | ||
@test FeatureTransforms.apply(x, t) == ones(3) | ||
|
||
M = reshape(1:9, 3, 3) | ||
@test FeatureTransforms.apply(M, t) == ones(3, 3) | ||
end | ||
|
||
@testset "FakeOneToManyTransform" begin | ||
t = FakeOneToManyTransform() | ||
@test cardinality(t) == OneToMany() | ||
|
||
x = [1, 2, 3] | ||
@test FeatureTransforms.apply(x, t) == ones(3, 2) | ||
|
||
M = reshape(1:9, 3, 3) | ||
@test FeatureTransforms.apply(M, t) == ones(3, 6) | ||
end | ||
|
||
@testset "FakeManyToOneTransform" begin | ||
t = FakeManyToOneTransform() | ||
@test cardinality(t) == ManyToOne() | ||
|
||
x = [1, 2, 3] | ||
@test FeatureTransforms.apply(x, t; dims=1) == fill(1) | ||
|
||
M = reshape(1:9, 3, 3) | ||
@test FeatureTransforms.apply(M, t; dims=1) == ones(3) | ||
end | ||
|
||
@testset "FakeManyToManyTransform" begin | ||
t = FakeManyToManyTransform() | ||
@test cardinality(t) == ManyToMany() | ||
|
||
x = [1, 2, 3] | ||
@test FeatureTransforms.apply(x, t) == ones(3, 2) | ||
|
||
M = reshape(1:9, 3, 3) | ||
@test FeatureTransforms.apply(M, t) == ones(3, 6) | ||
end | ||
|
||
|
||
@testset "is_transformable" begin | ||
|
||
# Test that AbstractArrays and Tables are transformable | ||
@test is_transformable([1, 2, 3, 4, 5]) | ||
@test is_transformable([1 2 3; 4 5 6]) | ||
@test is_transformable(AxisArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"])) | ||
@test is_transformable(KeyedArray([1 2 3; 4 5 6], foo=["a", "b"], bar=["x", "y", "z"])) | ||
@test is_transformable((a = [1, 2, 3], b = [4, 5, 6])) | ||
@test is_transformable(DataFrame(:a => [1, 2, 3], :b => [4, 5, 6])) | ||
|
||
# Test types that are not transformable | ||
@test is_transformable(1) == false | ||
@test is_transformable("string") == false | ||
@test is_transformable(true) == false | ||
@test is_transformable(Dict(2 => 3)) == false | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
eb9daee
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
eb9daee
There was a problem hiding this comment.
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/34497
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: