-
-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
662eb79
Added Container and Block
47544c7
WIP on Adding Support for Time Series
bc0f533
Added Adiac to list of datasets that can be downloaded
codeboy5 76b90c8
fixed some comments
codeboy5 9180b01
basic test added
codeboy5 4bea411
added support for loading multivariate datasets
codeboy5 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 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,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 |
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,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)) | ||
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 |
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,50 @@ | ||
Base.@kwdef struct TimeSeriesDatasetRecipe <: Datasets.DatasetRecipe | ||
file | ||
loadfn = loadfile | ||
end | ||
|
||
Datasets.recipeblocks(::Type{TimeSeriesDatasetRecipe}) = Tuple{TimeSeriesRow, Label} | ||
|
||
function Datasets.loadrecipe(recipe::TimeSeriesDatasetRecipe, path) | ||
path = convert(String, path) | ||
datasetpath = joinpath(path, recipe.file) | ||
rows, labels = recipe.loadfn(datasetpath) | ||
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.ts") | ||
], | ||
"ecg5000" => [ | ||
TimeSeriesDatasetRecipe(file="ECG5000_TRAIN.ts") | ||
], | ||
"natops" => [ | ||
TimeSeriesDatasetRecipe(file="NATOPS_TRAIN.ts") | ||
], | ||
) | ||
|
||
function _registerrecipes() | ||
for (name, recipes) in RECIPES, recipe in recipes | ||
Datasets.registerrecipe!(Datasets.FASTAI_DATA_REGISTRY, name, recipe) | ||
end | ||
end | ||
|
||
|
||
## Tests | ||
|
||
@testset "TimeSeriesDatasetRecipe [recipe]" begin | ||
path = datasetpath("adiac") | ||
recipe = TimeSeriesDatasetRecipe(file="Adiac_TRAIN.arff") | ||
data, block = loadrecipe(recipe, path) | ||
sample = getobs(data, 1) | ||
@test checkblock(block, sample) | ||
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
Oops, something went wrong.
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.
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: