Skip to content

Commit 13fde53

Browse files
committed
FEAT(plot): +_no_plot node/edge attribute
1 parent ee77fbc commit 13fde53

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

graphtik/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ def clone_or_merge_graph(self, base_graph) -> "PlotArgs":
397397
else:
398398
graph = base_graph.copy() # cloned, to freely annotate downstream
399399

400+
## Drop any nodes & edges with "_no_plot" attribute
401+
#
402+
graph.remove_nodes_from(
403+
[n for n, no_plot in graph.nodes.data("_no_plot") if no_plot]
404+
)
405+
graph.remove_edges_from(
406+
[
407+
(src, dst)
408+
for src, dst, no_plot in graph.edges.data("_no_plot")
409+
if no_plot
410+
]
411+
)
412+
400413
return self._replace(graph=graph)
401414

402415
def with_defaults(self, *args, **kw) -> "PlotArgs":
@@ -487,6 +500,7 @@ def plot(
487500
- ``_op_tooltip`` & ``_fn_tooltip`` *(node)*: if truthy,
488501
override those derrived from :meth:`_make_op_tooltip()` &
489502
:meth:`_make_op_tooltip()`.
503+
- ``_no_plot``: nodes and/or edges skipped from plotting
490504
491505
- "public" attributes: reaching `Graphviz`_ as-is.
492506

test/test_plot.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from graphtik import base, compose, network, operation, plot
1515
from graphtik.modifiers import optional
1616
from graphtik.netop import NetworkOperation
17+
from graphtik.network import yield_ops
1718
from graphtik.plot import (
1819
Plotter,
1920
Style,
@@ -477,7 +478,12 @@ def test_node_dot_str1(dot_str_pipeline, monkeypatch):
477478
monkeypatch.setattr(style, "op_link_target", "_self")
478479
monkeypatch.setattr(style, "fn_link_target", "bad")
479480

480-
dot_str = str(dot_str_pipeline.plot())
481+
## Test node-hidding & Graph-overlaying.
482+
#
483+
overlay = nx.DiGraph()
484+
hidden_op = dot_str_pipeline.net.find_op_by_name("node")
485+
overlay.add_node(hidden_op, _no_plot=True)
486+
dot_str = str(dot_str_pipeline.plot(graph=overlay))
481487
print(dot_str)
482488
exp = """
483489
digraph graph_ {
@@ -486,15 +492,6 @@ def test_node_dot_str1(dot_str_pipeline, monkeypatch):
486492
splines=ortho;
487493
<edge> [shape=invhouse];
488494
<digraph&#58; strict> [shape=invhouse];
489-
<node> [label=<<TABLE CELLBORDER="0" CELLSPACING="0" STYLE="rounded">
490-
<TR>
491-
<TD BORDER="1" SIDES="b" ALIGN="left" TOOLTIP="FunctionalOperation(name=&#x27;node&#x27;, needs=[&#x27;edge&#x27;, &#x27;digraph: strict&#x27;], provides=[&#x27;&lt;graph&gt;&#x27;], fn=&#x27;add&#x27;)" HREF="abc#{&#x27;dot_path&#x27;: &#x27;_operator.add&#x27;, &#x27;posix_path&#x27;: &#x27;_operator/add&#x27;}" TARGET="bad"
492-
><B>OP:</B> <I>node</I></TD>
493-
</TR><TR>
494-
<TD ALIGN="left" TOOLTIP="Same as a + b." HREF="abc#{&#x27;dot_path&#x27;: &#x27;_operator.add&#x27;, &#x27;posix_path&#x27;: &#x27;_operator/add&#x27;}" TARGET="bad"
495-
><B>FN:</B> &lt;built-in function add&gt;</TD>
496-
</TR>
497-
</TABLE>>, shape=plain, tooltip=<node>];
498495
<&lt;graph&gt;> [shape=house];
499496
<cu&#58;sto&#58;m> [label=<<TABLE CELLBORDER="0" CELLSPACING="0" STYLE="rounded">
500497
<TR>
@@ -505,11 +502,8 @@ def test_node_dot_str1(dot_str_pipeline, monkeypatch):
505502
><B>FN:</B> test.test_plot.func</TD>
506503
</TR>
507504
</TABLE>>, shape=plain, tooltip=<cu:sto:m>];
508-
<edge> -> <node>;
509505
<edge> -> <cu&#58;sto&#58;m>;
510-
<digraph&#58; strict> -> <node>;
511506
<digraph&#58; strict> -> <cu&#58;sto&#58;m>;
512-
<node> -> <&lt;graph&gt;>;
513507
<cu&#58;sto&#58;m> -> <&lt;graph&gt;>;
514508
legend [URL="https://graphtik.readthedocs.io/en/latest/_images/GraphtikLegend.svg", fillcolor=yellow, shape=component, style=filled, target=_top];
515509
}

0 commit comments

Comments
 (0)