Skip to content
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

Add label metadata to symbolic variables #2371

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/src/basics/Variable_metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ help?> u
Symbolics.VariableSource: (:variables, :u)
```

## Label

Label is descriptive name of a symbolic variable. It can be defined as:

```@example metadata
@parameters R [label = "Resistance"]
```

## Connect

Variables in connectors can have `connect` metadata which describes the type of connections.
Expand Down
2 changes: 1 addition & 1 deletion src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export @component, @mtkmodel, @mtkbuild
export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance,
istunable, getdist, hasdist,
tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar,
isintegervar
isintegervar, getlabel, haslabel
export ode_order_lowering, dae_order_lowering, liouville_transform
export PDESystem
export Differential, expand_derivatives, @derivatives
Expand Down
1 change: 1 addition & 0 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing)
metatypes = [(:connection_type, VariableConnectType),
(:description, VariableDescription),
(:label, VariableLabel),
(:unit, VariableUnit),
(:bounds, VariableBounds),
(:noise, VariableNoiseType),
Expand Down
19 changes: 19 additions & 0 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,25 @@
getdescription(x) != ""
end

## Label =================================================================
struct VariableLabel end
Symbolics.option_to_metadata_type(::Val{:label}) = VariableLabel

Check warning on line 377 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L377

Added line #L377 was not covered by tests

getlabel(x::Num) = getlabel(Symbolics.unwrap(x))

Check warning on line 379 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L379

Added line #L379 was not covered by tests

"""
getlabel(x)

Return labels attached to variables `x`. If no label is attached, an empty string is returned.
"""
function getlabel(x)
p = Symbolics.getparent(x, nothing)
p === nothing || (x = p)
Symbolics.getmetadata(x, VariableLabel, "")

Check warning on line 389 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L386-L389

Added lines #L386 - L389 were not covered by tests
end

haslabel(x) = (getlabel(x) != "")

Check warning on line 392 in src/variables.jl

View check run for this annotation

Codecov / codecov/patch

src/variables.jl#L392

Added line #L392 was not covered by tests

## binary variables =================================================================
struct VariableBinary end
Symbolics.option_to_metadata_type(::Val{:binary}) = VariableBinary
Expand Down
8 changes: 5 additions & 3 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ end
@testset "Metadata in variables" begin
metadata = Dict(:description => "Variable to test metadata in the Model.structure",
:input => true, :bounds => (-1, 1), :connection_type => :Flow, :integer => true,
:binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1))
:binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1),
:label => "MockVariable")

@connector MockMeta begin
m(t),
[description = "Variable to test metadata in the Model.structure",
input = true, bounds = (-1, 1), connect = Flow, integer = true,
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
label = "MockVariable", input = true, bounds = (-1, 1), connect = Flow,
integer = true, binary = false, tunable = false, disturbance = true,
dist = Normal(1, 1)]
end

for (k, v) in metadata
Expand Down
9 changes: 9 additions & 0 deletions test/test_variable_metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ sp = Set(p)
@test getdescription(u) == ""
@test !hasdescription(u)

## Labels
@variables l [label = "Label"]
@test getlabel(l) == "Label"
@test haslabel(l)

@variables l
@test getlabel(l) == ""
@test !haslabel(l)

@parameters t
@variables u(t) [description = "A short description of u"]
@parameters p [description = "A description of p"]
Expand Down
Loading