Skip to content
This repository was archived by the owner on May 23, 2022. It is now read-only.

add back iterators #47

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/LearnBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module LearnBase
# AGGREGATION MODES
include("aggmode.jl")

# VIEW AND ITERATORS
include("iteration.jl")

# OBSERVATION DIMENSIONS
export default_obsdim, getobs, getobs!
include("observation.jl")
Expand Down
82 changes: 82 additions & 0 deletions src/iteration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

"""
abstract DataView{TElem, TData} <: AbstractVector{TElem}

Baseclass for all vector-like views of some data structure.
This allow for example to see some design matrix as a vector of
individual observation-vectors instead of one matrix.
see `MLDataPattern.ObsView` and `MLDataPattern.BatchView` for examples.
"""
abstract type DataView{TElem, TData} <: AbstractVector{TElem} end

"""
abstract AbstractObsView{TElem, TData} <: DataView{TElem, TData}

Baseclass for all vector-like views of some data structure,
that views it as some form or vector of observations.
see `MLDataPattern.ObsView` for a concrete example.
"""
abstract type AbstractObsView{TElem, TData} <: DataView{TElem, TData} end

"""
abstract AbstractBatchView{TElem, TData} <: DataView{TElem, TData}

Baseclass for all vector-like views of some data structure,
that views it as some form or vector of equally sized batches.
see `MLDataPattern.BatchView` for a concrete example.
"""
abstract type AbstractBatchView{TElem, TData} <: DataView{TElem, TData} end

# --------------------------------------------------------------------

"""
abstract DataIterator{TElem,TData}

Baseclass for all types that iterate over a `data` source
in some manner. The total number of observations may or may
not be known or defined and in general there is no contract that
`getobs` or `nobs` has to be supported by the type of `data`.
Furthermore, `length` should be used to query how many elements
the iterator can provide, while `nobs` may return the underlying
true amount of observations available (if known).
see `MLDataPattern.RandomObs`, `MLDataPattern.RandomBatches`
"""
abstract type DataIterator{TElem,TData} end

"""
abstract ObsIterator{TElem,TData} <: DataIterator{TElem,TData}

Baseclass for all types that iterate over some data source
one observation at a time.
```julia
using MLDataPattern
@assert typeof(RandomObs(X)) <: ObsIterator
for x in RandomObs(X)
# ...
end
```
see `MLDataPattern.RandomObs`
"""
abstract type ObsIterator{TElem,TData} <: DataIterator{TElem,TData} end

"""
abstract BatchIterator{TElem,TData} <: DataIterator{TElem,TData}

Baseclass for all types that iterate over of some data source one
batch at a time.
```julia
@assert typeof(RandomBatches(X, size=10)) <: BatchIterator

for x in RandomBatches(X, size=10)
@assert nobs(x) == 10
# ...
end
```
see `MLDataPattern.RandomBatches`
"""
abstract type BatchIterator{TElem,TData} <: DataIterator{TElem,TData} end

# just for dispatch for those who care to
const AbstractDataIterator{E,T} = Union{DataIterator{E,T}, DataView{E,T}}
const AbstractObsIterator{E,T} = Union{ObsIterator{E,T}, AbstractObsView{E,T}}
const AbstractBatchIterator{E,T} = Union{BatchIterator{E,T},AbstractBatchView{E,T}}
4 changes: 4 additions & 0 deletions src/observation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ If it makes sense for the type of `data`, `obsdim` can be used
to disptach on which dimension of `data` denotes the observations.
"""
function datasubset end

# todeprecate
function target end
function gettarget end
23 changes: 23 additions & 0 deletions test/iteration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@test LearnBase.DataView <: AbstractVector
@test LearnBase.DataView <: LearnBase.AbstractDataIterator
@test LearnBase.DataView{Int} <: AbstractVector{Int}
@test LearnBase.DataView{Int,Vector{Int}} <: LearnBase.AbstractDataIterator{Int,Vector{Int}}
@test LearnBase.AbstractObsView <: LearnBase.DataView
@test LearnBase.AbstractObsView <: LearnBase.AbstractObsIterator
@test LearnBase.AbstractObsView{Int,Vector{Int}} <: LearnBase.DataView{Int,Vector{Int}}
@test LearnBase.AbstractObsView{Int,Vector{Int}} <: LearnBase.AbstractObsIterator{Int,Vector{Int}}
@test LearnBase.AbstractBatchView <: LearnBase.DataView
@test LearnBase.AbstractBatchView <: LearnBase.AbstractBatchIterator
@test LearnBase.AbstractBatchView{Int,Vector{Int}} <: LearnBase.DataView{Int,Vector{Int}}
@test LearnBase.AbstractBatchView{Int,Vector{Int}} <: LearnBase.AbstractBatchIterator{Int,Vector{Int}}

@test LearnBase.DataIterator <: LearnBase.AbstractDataIterator
@test LearnBase.DataIterator{Int,Vector{Int}} <: LearnBase.AbstractDataIterator{Int,Vector{Int}}
@test LearnBase.ObsIterator <: LearnBase.DataIterator
@test LearnBase.ObsIterator <: LearnBase.AbstractObsIterator
@test LearnBase.ObsIterator{Int,Vector{Int}} <: LearnBase.DataIterator{Int,Vector{Int}}
@test LearnBase.ObsIterator{Int,Vector{Int}} <: LearnBase.AbstractObsIterator{Int,Vector{Int}}
@test LearnBase.BatchIterator <: LearnBase.DataIterator
@test LearnBase.BatchIterator <: LearnBase.AbstractBatchIterator
@test LearnBase.BatchIterator{Int,Vector{Int}} <: LearnBase.DataIterator{Int,Vector{Int}}
@test LearnBase.BatchIterator{Int,Vector{Int}} <: LearnBase.AbstractBatchIterator{Int,Vector{Int}}
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ using Test
include("observation.jl")
end

@testset "Observation" begin
include("iteration.jl")
end

include("aggmode.jl")
include("costs.jl")
include("other.jl")