Skip to content

Commit 3f9af8b

Browse files
committed
Support for DataFrames.jl, Tables.jl, examples for DataFrames.jl and CSV.jl added
1 parent f031852 commit 3f9af8b

File tree

12 files changed

+166
-20
lines changed

12 files changed

+166
-20
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
88
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
99
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1010
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
11+
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1112
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1213
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
1314

@@ -16,5 +17,6 @@ DataFrames = "1, 0.22, 0.21, 0.20, 0.19"
1617
JSON = "0.21"
1718
OrderedCollections = "1"
1819
Pkg = "1"
20+
Tables = "1"
1921
Unitful = "1"
2022
julia = "1.7"

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ generate the following plot:
9898

9999
- [`SignalTable`](https://modiasim.github.io/SignalTables.jl/stable/Functions/SignalTables.html#SignalTables.SignalTable) (included in SignalTables.jl).
100100

101-
Planned implementations (basically adapting from [ModiaResult.jl](https://github.com/ModiaSim/ModiaResult.jl)):
101+
- [Modia.jl](https://github.com/ModiaSim/Modia.jl) (a modeling and simulation environment; version >= 0.9.0)
102+
103+
- [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl)
104+
(tabular data; first column is independent variable; *only scalar variables*))
102105

103-
- [Modia.jl](https://github.com/ModiaSim/Modia.jl) (a modeling and simulation environment)
104-
- [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl)
105-
(tabular data; first column is independent variable; *only scalar variables*))
106-
- [Tables.jl](https://github.com/JuliaData/Tables.jl)
107-
(abstract tables, e.g. [CSV](https://github.com/JuliaData/CSV.jl) tables;
108-
first column is independent variable; *only scalar variables*).
106+
- [Tables.jl](https://github.com/JuliaData/Tables.jl)
107+
(abstract tables, e.g. [CSV](https://github.com/JuliaData/CSV.jl) tables;
108+
first column is independent variable; *only scalar variables*).
109109

110110
*Concrete implementations* of the [Abstract Plot Interface](https://modiasim.github.io/SignalTables.jl/stable/Internal/AbstractPlotInterface.html) are provided for:
111111

docs/src/Examples/FileIO.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,43 @@ results in the following file:
5858

5959
- [VariousTypes.jld](../../resources/examples/fileIO/VariousTypes.jld).
6060

61+
62+
63+
64+
65+
## CSV - Read from File
66+
67+
All Julia tables that are derived from [Tables.jl](https://github.com/JuliaData/Tables.jl) are seen as signal tables.
68+
For example, a CSV Files is read from file and treated as signal Table:
69+
70+
```julia
71+
using SignalTables
72+
import CSV
73+
74+
file = joinpath(SignalTables.path, "examples", "fileIO", "Rotational_First.csv")
75+
76+
println("\n... Read csv file \"$file\"")
77+
sigTable = CSV.File(file)
78+
79+
println("\n... Show csv file as signal table")
80+
showInfo(sigTable)
81+
82+
println("\ntime[1:10] = ", getSignal(sigTable, "time")[:values][1:10])
83+
```
84+
85+
results in the following output:
86+
87+
```julia
88+
... Read csv file "[...]\SignalTables\examples\fileIO\Rotational_First.csv"
89+
90+
... Show csv file as signal table
91+
name unit size eltypeOrType kind attributes
92+
───────────────────────────────────────────────────────────────
93+
time (2002,) Float64 Var
94+
damper.phi_rel (2002,) Float64 Var
95+
damper.w_rel (2002,) Float64 Var
96+
inertia3.phi (2002,) Float64 Var
97+
inertia3.w (2002,) Float64 Var
98+
99+
time[1:10] = [0.0, 0.0005, 0.001, 0.0015, 0.002, 0.0025, 0.003, 0.0035, 0.004, 0.0045000000000000005]
100+
```

docs/src/Functions/OverviewOfFunctions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Note, *FileIO* functions (e.g. JSON, HDF5) can be directly used, see [FileIO Exa
5353
| [`getDefaultHeading`](@ref) | Returns the default heading for a plot. |
5454
| [`signalTableToJSON`](@ref) | Returns a JSON string representation of a [`SignalTable`](@ref) instance |
5555
| [`writeSignalTable`](@ref) | Write a [`SignalTable`](@ref) instance in JSON format on file. |
56+
| [`toSignalTable`](@ref) | Returns a signalTable as instance of [`SignalTable`](@ref). |
57+
| [`signalTableToDataFrame`](@ref) | Returns a signal table as [DataFrame](https://github.com/JuliaData/DataFrames.jl) object. |
5658

5759

5860
| Plot package functions | Description |

docs/src/Functions/SignalTables.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ The functions below operate on a *signal table* that implements the [Abstract Si
2929
| [`getDefaultHeading`](@ref) | Returns the default heading for a plot. |
3030
| [`signalTableToJSON`](@ref) | Returns a JSON string representation of a [`SignalTable`](@ref) instance |
3131
| [`writeSignalTable`](@ref) | Write a [`SignalTable`](@ref) instance in JSON format on file. |
32+
| [`toSignalTable`](@ref) | Returns a signalTable as instance of [`SignalTable`](@ref). |
33+
| [`signalTableToDataFrame`](@ref) | Returns a signal table as [DataFrame](https://github.com/JuliaData/DataFrames.jl) object. |
3234

3335

3436
```@docs
@@ -49,4 +51,6 @@ getFlattenedSignal
4951
getDefaultHeading
5052
signalTableToJSON
5153
writeSignalTable
54+
toSignalTable
55+
signalTableToDataFrame
5256
```

docs/src/index.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ generate the following plot:
9898

9999
- [`SignalTable`](@ref) (included in SignalTables.jl).
100100

101-
Planned implementations (basically adapting from [ModiaResult.jl](https://github.com/ModiaSim/ModiaResult.jl)):
102-
103-
- [Modia.jl](https://github.com/ModiaSim/Modia.jl) (a modeling and simulation environment)
104-
- [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl)
105-
(tabular data; first column is independent variable; *only scalar variables*))
106-
- [Tables.jl](https://github.com/JuliaData/Tables.jl)
107-
(abstract tables, e.g. [CSV](https://github.com/JuliaData/CSV.jl) tables;
108-
first column is independent variable; *only scalar variables*).
101+
- [Modia.jl](https://github.com/ModiaSim/Modia.jl) (a modeling and simulation environment; version >= 0.9.0)
102+
103+
- [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl)
104+
(tabular data; first column is independent variable; *only scalar variables*))
105+
106+
- [Tables.jl](https://github.com/JuliaData/Tables.jl)
107+
(abstract tables, e.g. [CSV](https://github.com/JuliaData/CSV.jl) tables;
108+
first column is independent variable; *only scalar variables*).
109109

110110
*Concrete implementations* of the [Abstract Plot Interface](@ref) are provided for:
111111

src/AbstractSignalTableInterface.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44
#
55
# This file is part of module SignalTables
66

7+
import Tables
8+
79
"""
810
isSignalTable(obj)::Bool
911
1012
Returns true, if `obj` is a signal table and supports the functions of the [Abstract Signal Table Interface](@ref).
1113
"""
12-
isSignalTable(obj) = false
14+
isSignalTable(obj) = Tables.istable(obj) && Tables.columnaccess(obj)
1315

1416

1517
"""
1618
independentSignalNames(signalTable)::Vector{String}
1719
1820
Returns the names of the independent signals (often: ["time"]) from signalTable.
1921
"""
20-
function independentSignalNames end
22+
function independentSignalNames(obj)
23+
if Tables.istable(obj) && Tables.columnaccess(obj)
24+
return [string(Tables.columnnames(obj)[1])]
25+
else
26+
@error "independentSignalNames(obj) is not supported for typeof(obj) = " * string(typeof(obj))
27+
end
28+
end
2129

2230

2331
"""
@@ -26,7 +34,13 @@ function independentSignalNames end
2634
Returns a string vector of the signal names that are present in signalTable
2735
(including independent signal names).
2836
"""
29-
function signalNames end
37+
function signalNames(obj)
38+
if Tables.istable(obj) && Tables.columnaccess(obj)
39+
return string.(Tables.columnnames(obj))
40+
else
41+
@error "signalNames(obj) is not supported for typeof(obj) = " * string(typeof(obj))
42+
end
43+
end
3044

3145

3246
"""
@@ -35,7 +49,13 @@ function signalNames end
3549
Returns signal `name` from `signalTable` (that is a [`Var`](@ref) or a [`Par`](@ref)).
3650
If `name` does not exist, an error is raised.
3751
"""
38-
function getSignal end
52+
function getSignal(obj, name::String)
53+
if Tables.istable(obj) && Tables.columnaccess(obj)
54+
return Var(values= Tables.getcolumn(obj, Symbol(name)))
55+
else
56+
@error "getSignal(obj, \"$name\") is not supported for typeof(obj) = " * string(typeof(obj))
57+
end
58+
end
3959

4060

4161
# ----------- Functions that have a default implementation ----------------------------------------

src/SignalTable.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,22 @@ function writeSignalTable(filename::String, signalTable::AbstractDict; indent=no
319319
end
320320
return nothing
321321
end
322+
323+
324+
325+
"""
326+
toSignalTable(signalTable)::SignalTable
327+
328+
Returns a signalTable as instance of [`SignalTable`](@ref).
329+
"""
330+
function toSignalTable(sigTable)::SignalTable
331+
if !isSignalTable(sigTable)
332+
error("toSignalTable(obj): obj::$(typeof(sigTable)) is no signal table.")
333+
end
334+
sigTable2 = SignalTable()
335+
for name in signalNames(sigTable)
336+
sigTable2[name] = getSignal(sigTable,name)
337+
end
338+
return sigTable2
339+
end
340+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Signal table interface for DataFrames
2+
3+
isSignalTable( obj::DataFrames.DataFrame) = true
4+
independentSignalNames( obj::DataFrames.DataFrame) = DataFrames.names(obj, 1)[1]
5+
getIndependentSignalsSize(obj::DataFrames.DataFrame) = [size(obj,1)]
6+
signalNames(obj::DataFrames.DataFrame) = DataFrames.names(obj)
7+
getSignal( obj::DataFrames.DataFrame, name::String) = Var(values = obj[!,name])
8+
hasSignal( obj::DataFrames.DataFrame, name::String) = haskey(obj, name)
9+
10+
11+
"""
12+
df = signalTableToDataFrame(signalTable)
13+
14+
Returns a signal table as [DataFrame](https://github.com/JuliaData/DataFrames.jl) object.
15+
"""
16+
function signalTableToDataFrame(sigTable)::DataFrames.DataFrame
17+
names = signalNames(sigTable)
18+
name = names[1]
19+
df = DataFrames.DataFrame(name = getSignal(sigTable,name)[:values])
20+
for i in 2:length(names)
21+
name = names[i]
22+
sig = getSignal(sigTable,name)
23+
if isVar(sig)
24+
sigValues = sig[:values]
25+
if typeof(sigValues) <: AbstractVector
26+
df[!,name] = sig[:values]
27+
else
28+
@info "$name::$(typeof(sigValues)) is ignored, because no Vector"
29+
end
30+
elseif isPar(sig)
31+
@info "$name is ignored, because Par(..) signal."
32+
else
33+
@info "$name::$(typeof(sig)) is ignored, because no Var(..) signal"
34+
end
35+
end
36+
return df
37+
end
38+
39+

src/SignalTables.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export isSignalTable, independentSignalNames, signalNames, hasSignal, getSignal,
2020
export new_signal_table, getValues, getValue, getValuesWithUnit, getValueWithUnit, getFlattenedSignal, showInfo, getHeading
2121

2222
# SignalTable
23-
export SignalTable, signalTableToJSON, writeSignalTable
23+
export SignalTable, signalTableToJSON, writeSignalTable, toSignalTable, signalTableToDataFrame
2424

2525
# Plot Package
2626
export @usingPlotPackage, usePlotPackage, usePreviousPlotPackage, currentPlotPackage
@@ -37,6 +37,7 @@ include("PlotPackageDefinition.jl")
3737
include("ExampleSignalTables.jl")
3838
include("NoPlot.jl")
3939
include("SilentNoPlot.jl")
40+
include("SignalTableInterface_DataFrames.jl")
4041

4142
#include("SilentNoPlot.jl")
4243
#include("UserFunctions.jl")

0 commit comments

Comments
 (0)