-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Added Time Series Container and Block #199
Changes from 3 commits
662eb79
47544c7
bc0f533
76b90c8
9180b01
4bea411
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module TimeSeries | ||
|
||
using ..FastAI | ||
using ..FastAI: | ||
# blocks | ||
Block, WrapperBlock, AbstractBlock, OneHotTensor, OneHotTensorMulti, Label, | ||
LabelMulti, wrapped, Continuous, getencodings, getblocks, | ||
# encodings | ||
Encoding, StatefulEncoding, OneHot, | ||
# visualization | ||
ShowText, | ||
# other | ||
FASTAI_METHOD_REGISTRY, registerlearningmethod! | ||
|
||
# for tests | ||
using ..FastAI: testencoding | ||
|
||
# extending | ||
import ..FastAI: | ||
blockmodel, blockbackbone, blocklossfn, encode, decode, checkblock, | ||
encodedblock, decodedblock, showblock!, mockblock, setup | ||
|
||
import Requires: @require | ||
import DataFrames: DataFrame, Not, select | ||
import UnicodePlots | ||
import ARFFFiles | ||
|
||
using FilePathsBase | ||
using InlineTest | ||
|
||
# Blocks | ||
include("blocks/timeseriesrow.jl") | ||
|
||
include("recipes.jl") | ||
|
||
export TimeSeriesRow | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
TimeSeriesRow{M,N}() <: Block | ||
|
||
`Block` for a M variate time series with N number of time steps. `obs` is valid for `TimeSeriesRow{M,N}()` | ||
if it is an (M,N) dimensional Matrix with number element type. | ||
|
||
## Examples | ||
|
||
Creating a block: | ||
|
||
```julia | ||
TimeSeriesRow{1,51}() # Univariate time series with length 51. | ||
TimeSeriesRow{2,51}() # Multivariate time series with 2 variables and length 51. | ||
``` | ||
""" | ||
struct TimeSeriesRow{M,N} <: Block end | ||
|
||
function checkblock(::TimeSeriesRow{M,N}, obs::AbstractArray{T,2}) where {M,N,T<:Number} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the above suggestion, we would also not have to dispatch on |
||
size(obs) == (M,N) | ||
end | ||
|
||
mockblock(::TimeSeriesRow{M,N}) where {M,N} = rand(Float64, (M,N)) | ||
|
||
function setup(::Type{TimeSeriesRow}, data) | ||
# N,M = size(data[1,:,:]) | ||
N,M = size(getobs(data, 1)) | ||
codeboy5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return TimeSeriesRow{N,M}() | ||
end | ||
|
||
# visualization | ||
|
||
function showblock!(io, ::ShowText, block::TimeSeriesRow, obs) | ||
plot = UnicodePlots.lineplot(obs[1,:]) | ||
for j=2:size(obs,1) | ||
UnicodePlots.lineplot!(plot, obs[j,:]) | ||
end | ||
print(io, plot) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Base.@kwdef struct TimeSeriesDatasetRecipe <: Datasets.DatasetRecipe | ||
file = "" | ||
codeboy5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
targetcol = "target" | ||
end | ||
|
||
Datasets.recipeblocks(::Type{TimeSeriesDatasetRecipe}) = Tuple{TimeSeriesRow, Label} | ||
|
||
function Datasets.loadrecipe(recipe::TimeSeriesDatasetRecipe, path) | ||
path = convert(String, path) | ||
datasetpath = joinpath(path, recipe.file) | ||
df = ARFFFiles.load(DataFrame, datasetpath) | ||
labels = Array(df[!, recipe.targetcol]) | ||
rows = Matrix(select(df, Not(:target))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine for now, but maybe there is a way to get an array view without copying everything. Not sure, though :) |
||
N,M = size(rows) | ||
rows = reshape(rows, (N,1,M)) | ||
rows = TimeSeriesDataset(rows) | ||
data = rows, labels | ||
blocks = ( | ||
setup(TimeSeriesRow,rows), | ||
Label(unique(eachobs(labels))), | ||
) | ||
|
||
return data, blocks | ||
end | ||
|
||
# Registering recipes | ||
|
||
const RECIPES = Dict{String,Vector{Datasets.DatasetRecipe}}( | ||
"adiac" => [ | ||
TimeSeriesDatasetRecipe(file="Adiac_TRAIN.arff") | ||
] | ||
) | ||
|
||
function _registerrecipes() | ||
for (name, recipes) in RECIPES, recipe in recipes | ||
Datasets.registerrecipe!(Datasets.FASTAI_DATA_REGISTRY, name, recipe) | ||
end | ||
end |
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.
Do we need these to be type parameters, i.e. are there places where we need to dispatch on them? See for example the dispatches on
Image{2}
for constructing models as an example where being able to dispatch helps.If not, I would suggest storing the sizes as fields, so something like: