Skip to content

Commit 2001727

Browse files
authored
Merge pull request #10 from JuliaDynamics/general_dataset
Dataset conversion improvement
2 parents 6b9a7db + 8eea0c8 commit 2001727

File tree

7 files changed

+72
-28
lines changed

7 files changed

+72
-28
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.3.2
2+
* Orders of magnitude speed-up in conversions between `Matrix` and `Dataset`,
3+
because now methods use `transpose` internally and only `reinterpret`.
4+
* Bugfix of `eltype` of `Reconstruction`.
5+
16
# v0.3.1
27
* Added `jacobian` function
38
* Removed `EomVector` nonsense.

src/dataset.jl

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ data[1] == data[1, :] # this is the first datapoint (D-dimensional)
6464
data[5, 3] # value of the third variable, at the 5th timepoint
6565
```
6666
67-
Use `Matrix(dataset)` to create a `Matrix`, and `Dataset(matrix)`
68-
to create a `Dataset` from a matrix. Notice: `Dataset(matrix)` assumes
69-
that each column of the matrix represents one dynamic variable. If instead each
70-
column of the matrix represents a datapoint, use `reinterpret(Dataset, matrix)`.
71-
72-
If you have various timeseries vectors `x, y, z, ...` pass them like
67+
Use `Matrix(dataset)` or `reinterpret(Matrix, dataset) to create a `Matrix`
68+
from a `dataset`, the first method returning a matrix where each column is a
69+
a timeseries while the second returning a matrix where each column is
70+
a datapoint. Similarly, use
71+
`Dataset(matrix)` or `reinterpret(Dataset, matrix)` to create a `Dataset` from
72+
a `matrix` that has structure as noted by the `Matrix` methods. Notice that the 2
73+
matrix versions are just the transpose of each other.
74+
75+
If you have various timeseries `Vector`s `x, y, z, ...` pass them like
7376
`Dataset(x, y, z, ...)`.
7477
7578
See also [`read_dataset`](@ref), [`write_dataset`](@ref) and [`minmaxima`](@ref).
@@ -113,22 +116,20 @@ end
113116

114117

115118
# Conversions:
116-
@inbounds function Base.convert(::Type{Matrix}, d::AbstractDataset{D,T}) where {D, T}
117-
mat = Matrix{T}(length(d), D)
118-
for i in 1:length(d)
119-
mat[i,:] .= d.data[i]
120-
end
121-
mat
119+
function Base.convert(::Type{Matrix}, d::AbstractDataset{D,T}) where {D, T}
120+
L = length(d)
121+
m = reinterpret(T, d.data, (D,L))
122+
transpose(m)
123+
end
124+
125+
function Base.reinterpret(::Type{M}, d::AbstractDataset{D,T}) where {M<:Matrix, D, T}
126+
L = length(d)
127+
reinterpret(T, d.data, (D,L))
122128
end
123129

124130
function Base.convert(::Type{Dataset}, mat::AbstractMatrix)
125-
D = size(mat, 2); T = eltype(mat)
126-
L = size(mat, 1)
127-
d = Vector{SVector{D, T}}(L)
128-
for i in 1:L
129-
d[i] = SVector{D, T}(view(mat, i, :))
130-
end
131-
return Dataset(d)
131+
m = transpose(mat)
132+
reinterpret(Dataset, m)
132133
end
133134

134135
function Base.reinterpret(::Type{Dataset}, mat::Array{T,2}) where {T<:Real}
@@ -142,8 +143,6 @@ function Base.convert(::Type{Dataset}, y::Vector{T}) where {T}
142143
return Dataset(data)
143144
end
144145

145-
146-
147146
### Pretty printing
148147
function matname(d::Dataset{D, T}) where {D, T}
149148
N = length(d)
@@ -236,7 +235,7 @@ end
236235
# Dataset IO #
237236
#####################################################################################
238237
"""
239-
read_dataset(file, V::Type{Dataset{D, T}}, delim::Char = '\t'; skipstart = 0)
238+
read_dataset(file, ::Type{<:Dataset}, delim::Char = '\t'; skipstart = 0)
240239
Read a `delim`-delimited text file directly into a dataset of dimension `D`
241240
with numbers of type `T`.
242241

src/discrete.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function Base.show(io::IO, ds::DiscreteDS{N, S, F, J}) where
254254
{N<:ANY, S<:ANY, F<:ANY, J<:ANY}
255255
text = "$(dimension(ds))-dimensional discrete system"
256256
print(io, text*"\n",
257-
" state: $(state(ds))\n", " eom: $F\n")
257+
" state: $(state(ds))\n", " e.o.m.: $F\n")
258258
end
259259

260260
@require Juno begin
@@ -274,7 +274,7 @@ function Base.show(io::IO, ds::BigDiscreteDS{T, F, J}) where
274274
{T, F<:ANY, J<:ANY}
275275
text = "$(dimension(ds))-dimensional Big discrete system"
276276
print(io, text*"\n",
277-
" state: $(state(ds))\n", " eom!: $F\n")
277+
" state: $(state(ds))\n", " e.o.m.: $F\n")
278278
end
279279

280280
@require Juno begin
@@ -291,7 +291,7 @@ end
291291
### 1-D
292292
function Base.show(io::IO, s::DiscreteDS1D{S, F, J}) where {S<:ANY, F<:ANY, J<:ANY}
293293
print(io, "1-dimensional discrete dynamical system:\n",
294-
"state: $(s.state)\n", "eom: $F\n")
294+
"state: $(s.state)\n", "e.o.m.: $F\n")
295295
end
296296
@require Juno begin
297297
function Juno.render(i::Juno.Inline, s::DiscreteDS1D{S, F, J}) where

src/reconstruction.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ end
5050
Reconstruction(s::AbstractVector{T}, D, τ) where {T} =
5151
Reconstruction{D, T, τ}(reconstruct(s, Val{D}(), τ))
5252

53-
@inline Base.eltype(::Reconstruction{D, T, t}) where {T,D,t} = T
5453
@inline delay(::Reconstruction{D, T, t}) where {T,D,t} = t
5554

5655
function reconstruct_impl(::Type{Val{D}}) where D

test/dataset_tests.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ using Base.Test, StaticArrays
1919
end
2020
end
2121

22+
@testset "Conversions" begin
23+
m = Matrix(data)
24+
@test Dataset(m) == data
25+
@test reinterpret(Dataset, reinterpret(Matrix, data)) == data
26+
@test transpose(m) == reinterpret(Matrix, data)
27+
28+
m = rand(1000, 4)
29+
@test reinterpret(Matrix, reinterpret(Dataset, m)) == m
30+
@test Dataset(m) !== Dataset(transpose(m))
31+
@test Dataset(m) == reinterpret(Dataset, transpose(m))
32+
end
33+
2234
@testset "IO" begin
2335
@test !isfile("test.txt")
2436
write_dataset("test.txt", data)
@@ -32,6 +44,19 @@ using Base.Test, StaticArrays
3244
@test dimension(data2) == 2
3345

3446
rm("test.txt")
47+
48+
write_dataset("test.txt", data, ',')
49+
@test isfile("test.txt")
50+
51+
data3 = read_dataset("test.txt", Dataset{3, Float64}, ',')
52+
@test dimension(data3) == 3
53+
@test data3 == data
54+
55+
data2 = read_dataset("test.txt", Dataset{2, Float64})
56+
@test dimension(data2) == 2
57+
58+
rm("test.txt")
59+
3560
@test !isfile("test.txt") # make extra sure!
3661
end
3762
end

test/reconstruction_tests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
println("\nTesting Reconstruction")
2+
if current_module() != DynamicalSystemsBase
3+
using DynamicalSystemsBase
4+
end
5+
using Base.Test, StaticArrays
6+
7+
@testset "Reconstruction" begin
8+
ds = Systems.towel()
9+
data = trajectory(ds, 10000)
10+
s = data[:, 1]; N = length(s)
11+
@testset "D = $(D), τ = $(τ)" for D in [2,3], τ in [2,3]
12+
13+
R = Reconstruction(s, D, τ)
14+
15+
@test R[(1+τ):end, 1] == R[1:end-τ, 2]
16+
@test size(R) == (length(s) - τ*(D - 1), D)
17+
end
18+
end

todo.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)