Skip to content

Commit 92c6029

Browse files
committed
enh(plot) pink-ify null pandas & np-arrays
1 parent cc0e457 commit 92c6029

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

graphtik/plot.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@
5858

5959
log = logging.getLogger(__name__)
6060

61+
## Gracefully import numpy & pandas to check for null values
62+
# (probable mistakes in function definitions).
63+
#
64+
_null_checks = []
65+
try:
66+
import numpy as np
67+
68+
def is_empty_array(val):
69+
return isinstance(val, np.ndarray) and not np.any()
70+
71+
_null_checks.append(is_empty_array)
72+
except ImportError:
73+
pass
74+
try:
75+
from pandas.core.generic import NDFrame
76+
77+
def is_empty_frame(val):
78+
return isinstance(val, NDFrame) and not val.any()
79+
80+
_null_checks.append(is_empty_frame)
81+
except ImportError:
82+
pass
6183

6284
#: A nested dictionary controlling the rendering of graph-plots in Jupyter cells,
6385
#:
@@ -1468,8 +1490,16 @@ def _make_node(self, plot_args: PlotArgs) -> pydot.Node:
14681490
if solution is not None:
14691491
if nx_node in solution:
14701492
styles.add("kw_data_in_solution")
1471-
if solution[nx_node] is None:
1472-
styles.add("kw_data_in_solution_null")
1493+
val = solution[nx_node]
1494+
for check in _null_checks:
1495+
try:
1496+
if check(val):
1497+
styles.add("kw_data_in_solution_null")
1498+
break
1499+
except Exception:
1500+
log.debug(
1501+
"Skipping null-check error on %s", val, exc_info=1
1502+
)
14731503

14741504
if nx_node in getattr(solution, "overwrites", ()):
14751505
styles.add("kw_data_overwritten")

test/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def test_node_dot_str1(dot_str_pipeline, monkeypatch):
622622
</TABLE>>, shape=invhouse, style=filled, tooltip="(input)\n(int) 2"];
623623
<&lt;graph&gt;> [fillcolor=SkyBlue, fixedsize=shape, label=<<TABLE CELLBORDER="0" CELLSPACING="0" BORDER="0">
624624
<TR><TD><graph></TD></TR>
625-
</TABLE>>, shape=house, style=filled, tooltip="(output)\n(None)\n(null-result)\n(x2 overwrites) 1. 3&#10; 0. None"];
625+
</TABLE>>, shape=house, style=filled, tooltip="(output)\n(None)\n(x2 overwrites) 1. 3&#10; 0. None"];
626626
<cu&#58;sto&#58;m> [label=<<TABLE CELLBORDER="0" CELLSPACING="0" STYLE="rounded" BGCOLOR="wheat">
627627
<TR>
628628
<TD BORDER="1" SIDES="b" ALIGN="left" TOOLTIP="FnOp(name=&#x27;cu:sto:m&#x27;, needs=[&#x27;edge&#x27;, &#x27;digraph: strict&#x27;], provides=[&#x27;&lt;graph&gt;&#x27;], fn=&#x27;func&#x27;)" TARGET="_top"

0 commit comments

Comments
 (0)