Skip to content

Commit

Permalink
Adding Table back in, removing info and schema (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlienart authored Jan 30, 2020
1 parent 24ae1b0 commit 02751d1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ language: julia
os:
- linux
julia:
- 1.0

This comment has been minimized.

Copy link
@tlienart

tlienart Jan 30, 2020

Author Collaborator
- 1.1
- 1.2
- 1.3
- 1.4
- nightly
matrix:
allow_failures:
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 = "ScientificTypes"
uuid = "321657f4-b219-11e9-178b-2701a2544e81"
authors = ["Anthony D. Blaom <anthony.blaom@gmail.com>"]
version = "0.6.1"
version = "0.7.0"

[compat]
julia = "1"
Expand Down
44 changes: 7 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Found
│ ├─ Image
│ │ ├─ ColorImage
│ │ └─ GrayImage
│ ├─ Table
│ └─ Textual
└─ Unknown
```
Expand All @@ -57,7 +58,7 @@ The steps below summarise the possible steps in defining such a convention:

* declare a new convention,
* declare new traits,
* implement custom `schema`, `show` and `info` functions,
* add new scientific types,
* add explicit `scitype` and `Scitype` definitions,
* define a `coerce` function.

Expand Down Expand Up @@ -105,46 +106,15 @@ end

### Adding scientific types

You may want to extend the type hierarchy defined above. In the case of the
MLJ convention, we consider a *table* as a scientific type:
You may want to extend the type hierarchy defined above. This is done as usual
with something like

```julia
struct Table{K} <: Known end
struct MyNewType{P} <: Known end
```

where `K` is a union over the scientific type of each of the columns.

### Implementing custom `schema`, `show` and `info`

If you have added new traits, you *may* want to extend the `schema` function
for objects with that trait. Subsequently you may also want to extend the
`show` of such schemas and the `info` of such objects.

The `Schema` constructor takes 4 tuples:
- the *names* of the features
- their *machine type*
- their *scientific type*
- the *number of rows*

In the MLJ convention:

```julia
function ScientificTypes.schema(X, ::Val{:table}; kw...)
sch = Tables.schema(X)
# ...
return Schema(names, types, stypes, nrows)
end
```

Extending the `show` or `info` is then straightforward.

```julia
ScientificTypes.info(X, ::Val{:table}) = schema(X)

function Base.show(io::IO, ::MIME"text/plain", s::ScientificTypes.Schema)
# ...
end
```
Recall that Scientific Types are only used for dispatching and so should not
have fields.

### Adding explicit `scitype` and `Scitype` definitions

Expand Down
47 changes: 25 additions & 22 deletions src/ScientificTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module ScientificTypes
# Type exports
export Scientific, Found, Unknown, Known, Finite, Infinite,
OrderedFactor, Multiclass, Count, Continuous, Textual,
Binary, ColorImage, GrayImage
export Convention, Schema
Binary, ColorImage, GrayImage, Table
export Convention

export scitype, scitype_union, elscitype, schema, info, nonmissing
export scitype, scitype_union, elscitype, nonmissing, trait

# -------------------------------------------------------------------
# Scientific Types
Expand All @@ -30,10 +30,11 @@ abstract type Found end
abstract type Known <: Found end
struct Unknown <: Found end

abstract type Infinite <: Known end
abstract type Finite{N} <: Known end
abstract type Infinite <: Known end
abstract type Finite{N} <: Known end
abstract type Image{W,H} <: Known end
struct Textual <: Known end
struct Textual <: Known end
struct Table{K} <: Known end

struct Continuous <: Infinite end
struct Count <: Infinite end
Expand Down Expand Up @@ -99,20 +100,6 @@ function trait(X)::Symbol
return :other
end

"""
info(X)
Return the metadata associated with some object `X`, typically a named tuple
keyed on a set of object traits.
*Notes on overloading:*: If the class of objects is detected by its type,
`info` can be overloaded in the usual way. If the class of objects is detected
by the value of `ScientificTypes.trait(object)` - say if this value is
`:some_symbol` - then one should define a method
`info(object, ::Val{:some_symbol})`.
"""
info(X) = info(X, Val(trait(X)))

# -----------------------------------------------------------------
# nonmissing

Expand All @@ -131,9 +118,25 @@ end
nonmissing = nonmissingtype

# -----------------------------------------------------------------
# includes
# Constructor for table scientific type

"""
Table(...)
Constructor for the `Table` scientific type with:
```
Table(S1, S2, ..., Sn) <: Table
```
where `S1, ..., Sn` are the scientific type of the table's columns which
are expected to be represented by abstract vectors.
"""
Table(Ts::Type{<:Scientific}...) = Table{<:Union{(Arr{<:T,1} for T in Ts)...}}

# -----------------------------------------------------------------
# scitype

include("scitype.jl")
include("schema.jl")

end # module
59 changes: 0 additions & 59 deletions src/schema.jl

This file was deleted.

2 changes: 1 addition & 1 deletion src/scitype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ If `tight=true` and `T>:Missing` then the function checks whether there are
"true missing values", otherwise it constructs a "tight copy" of the array
without a `Union{Missing,S}` type.
"""
function arr_scitype(A::Arr{T,N}, C::Convention, S;
function arr_scitype(A::Arr{T,N}, C::Convention, S::Type{<:Scientific};
tight::Bool=false) where {T,N}
# no explicit scitype available
S === Unknown && return Arr{scitype_union(A),N}
Expand Down
16 changes: 11 additions & 5 deletions test/convention.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ struct MockMLJ <: Convention end
@test_logs (:warn, "No convention specified. Did you forget to use the `set_convention` function?") (c = ST.convention())
@test c isa ST.NoConvention

struct MockMLJ <: Convention end
ST.set_convention(MockMLJ())
c = ST.convention()
@test c isa MockMLJ
Expand All @@ -19,12 +18,9 @@ end
isjunk(s::String) = s == "junk" ? true : false
ST.TRAIT_FUNCTION_GIVEN_NAME[:junk] = isjunk

ST.info(object, ::Val{:junk}) = length(object)

X = (x = [1,2,3],
y = [5,5,7])
@test ST.trait(X) == :table
@test info("junk") == 4
@test ST.trait(X) == :table
X = [1,2,3]
@test ST.trait(X) == :other
end
Expand All @@ -33,3 +29,13 @@ end
U = Union{Missing,Int}
@test nonmissing(U) == Int
end

@testset "table" begin
T0 = Table(Continuous)
@test T0 == Table{K} where K<:AbstractVector{<:Continuous}
T1 = Table(Continuous, Count)
@test T1 == Table{K} where K<:Union{AbstractVector{<:Continuous}, AbstractVector{<:Count}}
T2 = Table(Continuous, Union{Missing,Continuous})
@test T2 == Table{K} where K<:Union{AbstractVector{<:Union{Missing,Continuous}}}
@test_throws MethodError Table(Int,Float64)
end
2 changes: 0 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ const ST = ScientificTypes

include("convention.jl")

include("schema.jl")

include("scitype.jl")
19 changes: 0 additions & 19 deletions test/schema.jl

This file was deleted.

1 comment on commit 02751d1

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

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.0 -m "<description of version>" 02751d1e7aaf31c531c3c1d91ca840a4d4f3b88b
git push origin v0.7.0

Please sign in to comment.