-
Notifications
You must be signed in to change notification settings - Fork 8
Light interface #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
*DS_Store | ||
Manifest.toml |
This file contains hidden or 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,18 @@ | ||
# Documentation: http://docs.travis-ci.com/user/languages/julia/ | ||
language: julia | ||
os: | ||
- linux | ||
julia: | ||
- 1.1 | ||
- 1.2 | ||
- 1.3 | ||
- 1.4 | ||
- nightly | ||
matrix: | ||
allow_failures: | ||
- julia: nightly | ||
notifications: | ||
email: false | ||
after_success: | ||
# push coverage results to Codecov | ||
- julia -e 'using Pkg; pkg"add Coverage"; using Coverage; Codecov.submit(Codecov.process_folder())' |
File renamed without changes.
This file contains hidden or 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,6 @@ | ||
# This file is machine-generated - editing it directly is not advised | ||
|
||
[[ScientificTypes]] | ||
git-tree-sha1 = "661a775ebf854f14509f590952eba63b47c82a5f" | ||
uuid = "321657f4-b219-11e9-178b-2701a2544e81" | ||
version = "0.6.0" |
This file contains hidden or 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,17 @@ | ||
name = "MLJModelInterface" | ||
uuid = "e80e1ace-859a-464e-9ed9-23947d8ae3ea" | ||
authors = ["Thibaut Lienart <ltib@me.com>"] | ||
version = "0.1.0" | ||
|
||
[deps] | ||
ScientificTypes = "321657f4-b219-11e9-178b-2701a2544e81" | ||
|
||
[compat] | ||
ScientificTypes = "^0.6" | ||
|
||
[extras] | ||
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test", "Tables"] |
This file contains hidden or 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,47 @@ | ||
module MLJModelInterface | ||
|
||
# ------------------------------------------------------------------------ | ||
# Dependency (note that ScientificTypes itself does not have dependencies) | ||
import ScientificTypes: trait | ||
|
||
# ------------------------------------------------------------------------ | ||
# Single export: matrix, everything else is qualified in MLJBase | ||
export matrix | ||
|
||
# ------------------------------------------------------------------------ | ||
|
||
abstract type Mode end | ||
struct LightInterface <: Mode end | ||
struct FullInterface <: Mode end | ||
|
||
const INTERFACE_MODE = Ref{Mode}(LightInterface()) | ||
|
||
set_interface_mode(m::Mode) = (INTERFACE_MODE[] = m) | ||
|
||
get_interface_mode() = INTERFACE_MODE[] | ||
|
||
struct InterfaceError <: Exception | ||
m::String | ||
end | ||
|
||
vtrait(X) = X |> trait |> Val | ||
|
||
""" | ||
matrix(X; transpose=false) | ||
|
||
If `X <: AbstractMatrix`, return `X` or `permutedims(X)` if `transpose=true`. | ||
If `X` is a Tables.jl compatible table source, convert `X` into a `Matrix`. | ||
""" | ||
matrix(X; kw...) = matrix(vtrait(X), X, get_interface_mode(); kw...) | ||
|
||
matrix(::Val{:other}, X::AbstractMatrix, ::Mode; transpose=false) = | ||
transpose ? permutedims(X) : X | ||
|
||
matrix(::Val{:other}, X, ::Mode; kw...) = | ||
throw(ArgumentError("Function `matrix` only supports AbstractMatrix or " * | ||
"containers implementing the Tables interface.")) | ||
|
||
matrix(::Val{:table}, X, ::LightInterface; kw...) = | ||
throw(InterfaceError("Only `MLJModelInterface` loaded. Import `MLJBase`.")) | ||
|
||
end # module |
This file contains hidden or 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,41 @@ | ||
using Test, MLJModelInterface, ScientificTypes | ||
using Tables | ||
|
||
const M = MLJModelInterface | ||
|
||
ScientificTypes.TRAIT_FUNCTION_GIVEN_NAME[:table] = Tables.istable | ||
|
||
@testset "light-interface" begin | ||
M.set_interface_mode(M.LightInterface()) | ||
@test M.get_interface_mode() isa M.LightInterface | ||
|
||
# matrix object (:other) | ||
X = zeros(3, 4) | ||
mX = matrix(X) | ||
mtX = matrix(X; transpose=true) | ||
|
||
@test mX === X | ||
@test mtX == permutedims(X) | ||
|
||
# :other but not matrix | ||
X = (1, 2, 3, 4) | ||
@test_throws ArgumentError matrix(X) | ||
|
||
# :table | ||
X = (x=[1,2,3], y=[1,2,3]) | ||
@test M.vtrait(X) isa Val{:table} | ||
@test_throws M.InterfaceError matrix(X) | ||
end | ||
|
||
@testset "full-interface" begin | ||
M.set_interface_mode(M.FullInterface()) | ||
@test M.get_interface_mode() isa M.FullInterface | ||
|
||
M.matrix(::Val{:table}, X, ::M.FullInterface; kw...) = | ||
Tables.matrix(X; kw...) | ||
|
||
X = (x=[1,2,3], y=[1,2,3]) | ||
mX = matrix(X) | ||
@test mX isa Matrix | ||
@test mX == hcat(X.x, X.y) | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.