Skip to content

Commit 428b3d7

Browse files
committed
improve printing of FilteringProxy
1 parent caa612a commit 428b3d7

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/show.jl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,19 @@ function Base.show(io::IO, mime::MIME"text/plain", fp::FilteringProxy)
453453
end
454454
else
455455
printstyled(io, "\nNo indices matching filter!", bold=true)
456+
if !isnothing(fp.compfilter) || !isnothing(fp.varfilter)
457+
print(" Filtering for ")
458+
fp.states && printstyled(io, "s ", color=:light_green)
459+
fp.parameters && printstyled(io, "p ", color=:light_yellow)
460+
fp.inputs && printstyled(io, "in ", color=:light_magenta)
461+
fp.outputs && printstyled(io, "out ", color=:light_blue)
462+
fp.observables && printstyled(io, "obs ", color=:light_cyan)
463+
if !isnothing(fp.varfilter)
464+
print(io, "\n - ", _explain_varfilter(fp.varfilter))
465+
end
466+
printstyled(io, " within", color=:light_black)
467+
print(io, "\n - ", _explain_compfilter(fp.compfilter))
468+
end
456469
end
457470
end
458471
_print_pattern_hl(io, s, pattern; kw...) = false
@@ -525,6 +538,60 @@ function _show_component_filter(io::IO, vs::Union{AbstractVector, Tuple})
525538
end
526539
print(io, post)
527540
end
541+
_explain_compfilter(x) = repr(x) # fallback
542+
function _explain_compfilter(cf::AbstractArray)
543+
join(map(_explain_compfilter, cf), " or ")
544+
end
545+
function _explain_compfilter(cf::VIndex{<:Symbol})
546+
"vertices named :$(cf.compidx)"
547+
end
548+
function _explain_compfilter(cf::EIndex{<:Symbol})
549+
"edges named :$(cf.compidx)"
550+
end
551+
function _explain_compfilter(cf::VIndex{<:Integer})
552+
"vertex #$(cf.compidx)"
553+
end
554+
function _explain_compfilter(cf::EIndex{<:Integer})
555+
"edge #$(cf.compidx)"
556+
end
557+
function _explain_compfilter(cf::EIndex{<:Union{AbstractString,Regex}})
558+
"edges with name containing $(repr(cf.compidx))"
559+
end
560+
function _explain_compfilter(cf::VIndex{<:Union{AbstractString,Regex}})
561+
"vertices with name containing $(repr(cf.compidx))"
562+
end
563+
function _explain_compfilter(cf::VIndex{<:UnitRange})
564+
"vertices $(first(cf.compidx))-$(last(cf.compidx))"
565+
end
566+
function _explain_compfilter(cf::EIndex{<:UnitRange})
567+
"edges $(first(cf.compidx))-$(last(cf.compidx))"
568+
end
569+
function _explain_compfilter(cf::EIndex{<:Pair})
570+
"edge $(cf.compidx.first) => $(cf.compidx.second)"
571+
end
572+
_explain_compfilter(cf::EIndex{Colon}) = "any edge"
573+
_explain_compfilter(cf::VIndex{Colon}) = "any vertex"
574+
function _explain_compfilter(cf::SymbolicIndex{<:Union{AbstractVector, Tuple}})
575+
if length(cf.compidx) > 3
576+
repr(cf)
577+
else
578+
join(map(x->_explain_compfilter(idxtype(cf)(x)), cf.compidx), " or ")
579+
end
580+
end
581+
_explain_compfilter(cf::AllVertices) = "any vertex"
582+
_explain_compfilter(cf::AllEdges) = "any edge"
583+
_explain_compfilter(::Nothing) = "any components"
584+
585+
_explain_varfilter(x) = repr(x) # fallback
586+
_explain_varfilter(::Nothing) = "any symbol" # fallback
587+
_explain_varfilter(x::AbstractArray) = join(map(_explain_varfilter, x), " or ")
588+
function _explain_varfilter(x::Symbol)
589+
"symbols named :$x"
590+
end
591+
function _explain_varfilter(x::Union{Regex, AbstractString})
592+
"symbols containing $(repr(x))"
593+
end
594+
528595

529596

530597
function print_treelike(io, vec; prefix=" ", infix=" ", rowmax=typemax(Int))

test/symbolicindexing_test.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@ end
226226
@test s.v[1, StateIdx(:)] == [4, 5]
227227
s.v[1, StateIdx(:)] = [7,8]
228228
@test s.v[1, StateIdx(:)] == [7, 8]
229+
230+
s.v(varfilter=r"foo")
231+
FilteringProxy(s)(varfilter="foo")
232+
NetworkDynamics._explain_compfilter(VIndex(1:2))
233+
NetworkDynamics._explain_compfilter(VIndex(:concrete_name))
234+
NetworkDynamics._explain_compfilter([VIndex(:concrete_name), EIndex(:)])
235+
NetworkDynamics._explain_compfilter([VIndex(:concrete_name), EIndex(r"foo")])
236+
NetworkDynamics._explain_compfilter(VIndex(["foo", r"bar"]))
237+
s.v["foo"]
238+
s.e[:](r"unmatched_regex")
239+
s.v[["foo", 1:2, r"bar"]](["foo", :bar])
229240
end
230241

231242
@testset "Test colon indexing" begin

0 commit comments

Comments
 (0)