Signal Tables

The functions below operate on a signal table that implements the Abstract Signal Table Interface.

Note

SignalTables.jl exports all symbols of this table.
Modia.jl reexports all symbols and uses instantiatedModel as signalTable argument.

Signal table functionsDescription
new_signal_tableReturns a new signal table dictionary (= OrderedDict{String,Any}("_class" => :SignalTable)).
SignalTableReturns a new instance of type SignalTable.
showInfoWrites info about a signal table to the output stream.
getIndependentSignalNamesReturns the names of the independent signals.
getSignalNamesReturns a string vector of the signal names that are present in a signal table.
hasSignalReturns true if a signal is present in a signal table.
getSignalReturns signal from a signal table as Var, Par or Map.
getSignalInfoReturns signal with :_typeof, :_size keys instead of :values/:value keys.
getIndependentSignalsSizeReturns the lengths of the independent signals as Dims.
getValuesReturns the values of a Var signal from a signal table.
getValuesWithUnitReturns the values of a Var signal from a signal table including its unit.
getValueReturns the value of a Par signal from a signal table.
getValueWithUnitReturns the value of a Par signal from a signal table including its unit.
getFlattenedSignalReturns a copy of a signal where the values or the value are flattened and converted for use in plots or traditional tables.
getDefaultHeadingReturns the default heading for a plot.
signalTableToJSONReturns a JSON string representation of a signal table.
writeSignalTableWrite a signal table in JSON format on file.
toSignalTableReturns a signal table as instance of SignalTable.
signalTableToDataFrameReturns a signal table as DataFrame object.
SignalTables.new_signal_tableFunction
new_signal_table(args...)::OrderedDict{String,Any}

Returns a new signal table, that is OrderedDict{String,Any}("_class" => :SignalTable, args...)

source
SignalTables.SignalTableType
sigTable = SignalTable(args...)

Returns a new SignalTable dictionary.

Arguments args... are dictionary pairs where values must be Var(...) and/or Par(...) and/or Map(...). Example:

The first argument must define the independent signal, that is, Var(values=..., independent=true), ... and values must be an AbstractVector. Further added signals with a :values key, must have the same first dimension as the independent signal.

Most dictionary operations can be applied on sigTable, as well as all functions of Overview of Functions.

Examples

using SignalTables
using Unitful

t = 0.0:0.1:0.5
sigTable = SignalTable(
  "time"         => Var(values= t, unit="s", independent=true),
  "load.r"       => Var(values= [sin.(t) cos.(t) sin.(t)], unit="m"),
  "motor.angle"  => Var(values= sin.(t), unit="rad", state=true, der="motor.w"),
  "motor.w"      => Var(values= cos.(t), unit="rad/s"),
  "motor.w_ref"  => Var(values= 0.9*[sin.(t) cos.(t)], unit = ["rad", "1/s"],
                                info="Reference angle and speed"),
  "wm"           => Var(alias = "motor.w"),
  "ref.clock"    => Var(values= [true, missing, missing, true, missing, missing],
                                 variability="clock"),
  "motor.w_c"    => Var(values= [0.8, missing, missing, 1.5, missing, missing],
                                variability="clocked", clock="ref.clock"),
  "motor.inertia"=> Par(value = 0.02f0, unit="kg*m/s^2"),
  "motor.data"   => Par(value = "resources/motorMap.json"),
  "attributes"   => Map(experiment=Map(stoptime=0.5, interval=0.01))
)

signalInfo(sigTable)

This results in the following output:

name          unit          size  eltypeOrType            kind attributes
─────────────────────────────────────────────────────────────────────────────────────────────────────────
time          "s"            [6]   Float64                Var  independent=true
load.r        "m"            [6,3] Float64                Var
motor.angle   "rad"          [6]   Float64                Var  state=true, der="motor.w"
motor.w       "rad/s"        [6]   Float64                Var
motor.w_ref   ["rad", "1/s"] [6,2] Float64                Var  info="Reference angle and speed"
wm            "rad/s"        [6]   Float64                Var  alias="motor.w"
ref.clock                    [6]   Union{Missing,Bool}    Var  variability="clock"
motor.w_c                    [6]   Union{Missing,Float64} Var  variability="clocked", clock="ref.clock"
motor.inertia "kg*m/s^2"           Float32                Par
motor.data                         String                 Par
attributes                                                Map  experiment=Map(stoptime=0.5, interval=0.01)

The command show(IOContext(stdout, :compact => true), sigTable) results in the following output:

SignalTable(
  "time" => Var(values=0.0:0.1:0.5, unit="s", independent=true),
  "load.r" => Var(values=[0.0 1.0 0.0; 0.0998334 0.995004 0.0998334; 0.198669 0.980067 0.198669; 0.29552 0.955336 0.29552; 0.389418 0.921061 0.389418; 0.479426 0.877583 0.479426], unit="m"),
  "motor.angle" => Var(values=[0.0, 0.0998334, 0.198669, 0.29552, 0.389418, 0.479426], unit="rad", state=true. der="motor.w"),
  "motor.w" => Var(values=[1.0, 0.995004, 0.980067, 0.955336, 0.921061, 0.877583], unit="rad/s"),
  "motor.w_ref" => Var(values=[0.0 0.9; 0.0898501 0.895504; 0.178802 0.88206; 0.265968 0.859803; 0.350477 0.828955; 0.431483 0.789824], unit=["rad", "1/s"], info="Reference angle and speed"),
  "wm" => Var(values=[1.0, 0.995004, 0.980067, 0.955336, 0.921061, 0.877583], unit="rad/s", alias="motor.w"),
  "ref.clock" => Var(values=Union{Missing, Bool}[true, missing, missing, true, missing, missing], variability="clock"),
  "ref.trigger" => Var(values=Union{Missing, Bool}[missing, missing, true, missing, true, true], variability="trigger"),
  "motor.w_c" => Var(values=Union{Missing, Float64}[0.8, missing, missing, 1.5, missing, missing], variability="clocked", clock="ref.clock"),
  "motor.inertia" => Par(value=0.02, unit="kg*m/s^2"),
  "motor.data" => Par(value="resources/motorMap.json"),
  "attributes" => Map(experiment=Map(stoptime=0.5, interval=0.01)),
  )
source
SignalTables.showInfoFunction
showInfo([io::IO=stdout,] signalTable;
         sorted=false, showVar=true, showPar=true, showMap=true, showAttributes=true)

Writes info about a signal table to the output stream. The keyword arguments define what information shall be printed or whether the names shall be sorted or presented in definition order.

Example

using SignalTables
using Unitful

t = 0.0:0.1:0.5
sigTable = SignalTable(
  "time"         => Var(values= t, unit="s", independent=true),
  "load.r"       => Var(values= [sin.(t) cos.(t) sin.(t)], unit="m"),
  "motor.angle"  => Var(values= sin.(t), unit="rad", state=true, der="motor.w"),
  "motor.w"      => Var(values= cos.(t), unit="rad/s"),
  "motor.w_ref"  => Var(values= 0.9*[sin.(t) cos.(t)], unit = ["rad", "1/s"],
                                info="Reference angle and speed"),
  "wm"           => Var(alias = "motor.w"),
  "ref.clock"    => Var(values= [true, missing, missing, true, missing, missing],
                                 variability="clock"),
  "motor.w_c"    => Var(values= [0.8, missing, missing, 1.5, missing, missing],
                                variability="clocked", clock="ref.clock"),
  "motor.inertia"=> Par(value = 0.02f0, unit="kg*m/s^2"),
  "motor.data"   => Par(value = "resources/motorMap.json"),
  "attributes"   => Map(experiment=Map(stoptime=0.5, interval=0.01))
)

signalInfo(sigTable)

results in the following output

name          unit           size  eltypeOrType           kind attributes
───────────────────────────────────────────────────────────────────────────────────────────────────────
time          "s"            [6]   Float64                Var independent=true
load.r        "m"            [6,3] Float64                Var
motor.angle   "rad"          [6]   Float64                Var state=true, der="motor.w"
motor.w       "rad/s"        [6]   Float64                Var
motor.w_ref   ["rad", "1/s"] [6,2] Float64                Var info="Reference angle and speed"
wm            "rad/s"        [6]   Float64                Var alias="motor.w"
ref.clock                    [6]   Union{Missing,Bool}    Var variability="clock"
motor.w_c                    [6]   Union{Missing,Float64} Var variability="clocked", clock="ref.clock"
motor.inertia "kg*m/s^2"           Float32                Par
motor.data                         String                 Par
attributes                                                Map experiment=Map(stoptime=0.5, interval=0.01)
source
SignalTables.getSignalNamesFunction
getSignalNames(signalTable; getVar=true, getPar=true, getMap=true)::Vector{String}

Returns a string vector of the signal names that are present in signalTable (including independent signal names).

  • If getVar=true, Var(..) variables are included.
  • If getPar=true, Par(..) variables are included.
  • If getMap=true, Map(..) variables are included.
source
SignalTables.getSignalFunction
getSignal(signalTable, name::String)

Returns signal name from signalTable (that is a Var, a Par or a Map). If name does not exist, an error is raised.

source
SignalTables.getSignalInfoFunction
getSignalInfo(signalTable, name::String)

Returns signal, but Var or Par) without :values or :value but instead with :eltypeOrType (eltype of the values if AbstractArray, otherwise typeof the values) and :size (if defined on the values)

If name does not exist, an error is raised.

This function is useful if only the attributes of a signal are needed, but not their values (returning the attributes might be a cheap operation, whereas returning the values might be an expensive operation).

source
SignalTables.getIndependentSignalsSizeFunction
getIndependentSignalsSize(signalTable)::Dims

Returns the lengths of the independent signals as Dims. E.g. for one independent signal of length 5 return (5,), or for two independent signals of length 5 and 7 return (5,7).

source
SignalTables.getFlattenedSignalFunction
signal = getFlattenedSignal(signalTable, name;
                            missingToNaN = true,
                            targetInt    = Int,
                            targetFloat  = Float64)

Returns a copy of a signal where the flattened and converted values (e.g.: missing -> NaN) are stored as signal[:flattenedValues] and the legend as signal[:legend]. A flattened signal can be, for example, used for traditional plot functions or for traditional tables.

signal[:flattenedValues] is a reshape of values into a vector or a matrix with optionally the following transformations:

  • name can be a signal name with or without array range indices (for example name = "a.b.c[2,3:5]").
  • If missingToNaN=true, then missing values are replaced by NaN values. If NaN does not exist in the corresponding type, the values are first converted to targetFloat.
  • If targetInt is not nothing, Int-types are converted to targetInt
  • If targetFloat is not nothing, Float-types are converted to targetFloat
  • collect(..) is performed on the result.

flattenedSignal[:legend] is a vector of strings that provides a description for every array column (e.g. if "name=a.b.c[2,3:5]", unit="m/s", then legend = ["a.b.c[2,3] [m/s]", "a.b.c[2,3] [m/s]", "a.b.c[2,5] [m/s]"].

If the required transformation is not possible, a warning message is printed and nothing is returned.

As a special case, if signal[:values] is a vector or signal[:value] is a scalar and an element of values or value is of type Measurements{targetFloat} or MonteCarloMeasurements{targetFloat}, then the signal is not transformed, so signal[:flattenedValues] = signal[:values].

source
SignalTables.signalTableToJSONFunction
json = signalTableToJSON(signalTable; signalNames = nothing)

Returns a JSON string representation of signalTable

If keyword signalNames with a Vector of strings is provided, then a signal table with the corresponding signals are returned as JSON string.

source
SignalTables.writeSignalTableFunction
writeSignalTable(filename::String, signalTable; signalNames=nothing, indent=nothing, log=false)

Write signalTable in JSON format on file filename.

If keyword signalNames with a Vector of strings is provided, then a signal table with the corresponding signals are stored on file.

If indent=<number> is given, then <number> indentation is used (otherwise, compact representation)

source