Skip to content

Commit 0ebd469

Browse files
[Feature] Improve plotting by using short names (#900)
* [Feature] Improve plotting by using short names * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add a test * handle cases all on one level * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Always shorten name * reduce classes * fix tests * Convert multi line entries to class string * cover all lines of new function * Catch multi line first --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1ad100c commit 0ebd469

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

src/executorlib/task_scheduler/interactive/dependency_plot.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ def plot_dependency_graph_function(
218218
graph = nx.DiGraph()
219219
for node in node_lst:
220220
if node["type"] == "input":
221-
graph.add_node(node["id"], label=str(node["value"]), shape=node["shape"])
221+
graph.add_node(
222+
node["id"],
223+
label=_short_object_name(node=node["value"]),
224+
shape=node["shape"],
225+
)
222226
else:
223227
graph.add_node(node["id"], label=str(node["name"]), shape=node["shape"])
224228
for edge in edge_lst:
@@ -306,3 +310,31 @@ def export_dependency_graph_function(
306310
}
307311
with open(file_name, "w") as f:
308312
json.dump(pwd_dict, f, indent=4)
313+
314+
315+
def _short_object_name(node):
316+
node_value_str = str(node)
317+
if isinstance(node, tuple):
318+
short_name = str(tuple(_short_object_name(node=el) for el in node))
319+
elif isinstance(node, list):
320+
short_name = str([_short_object_name(node=el) for el in node])
321+
elif isinstance(node, dict):
322+
short_name = str(
323+
{
324+
_short_object_name(node=key): _short_object_name(node=value)
325+
for key, value in node.items()
326+
}
327+
)
328+
elif "object at" in node_value_str:
329+
short_name = node_value_str[1:-1].split()[0].split(".")[-1] + "()"
330+
elif "<function" in node_value_str:
331+
short_name = node_value_str.split()[1] + "()"
332+
elif "\n" in node_value_str:
333+
short_name = str(type(node)).split("'")[1].split(".")[-1] + "()"
334+
elif "(" in node_value_str and ")" in node_value_str:
335+
short_name = node_value_str.split("(")[0] + "()"
336+
elif len(node_value_str) > 20:
337+
short_name = node_value_str[:21] + "..."
338+
else:
339+
short_name = node_value_str
340+
return short_name
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest
2+
import numpy as np
3+
from executorlib.task_scheduler.interactive.dependency_plot import _short_object_name
4+
5+
6+
class MyClass:
7+
def __init__(self, i):
8+
self._i = i
9+
10+
11+
class MyClassStr:
12+
def __init__(self, i):
13+
self._i = i
14+
15+
def __str__(self):
16+
return "MyClassStr(i=" + str(self._i) + ")"
17+
18+
19+
def my_function(i):
20+
return i
21+
22+
23+
class TestShortObjectName(unittest.TestCase):
24+
def test_short_object_name(self):
25+
result = _short_object_name(node=[MyClass("a"), MyClass("b")])
26+
self.assertEqual("['MyClass()', 'MyClass()']", result)
27+
result = _short_object_name(node=(MyClass("a"), MyClass("b")))
28+
self.assertEqual("('MyClass()', 'MyClass()')", result)
29+
result = _short_object_name(node=[MyClassStr("a"), MyClassStr("b")])
30+
self.assertEqual("['MyClassStr()', 'MyClassStr()']", result)
31+
result = _short_object_name(node=[np.array([[1,2], [4,3]]), np.array([[1,2], [4,3]])])
32+
self.assertEqual("['ndarray()', 'ndarray()']", result)
33+
result = _short_object_name(node={"this is a very long string far too long for a dictionary key": my_function})
34+
self.assertEqual("{'this is a very long s...': 'my_function()'}", result)

0 commit comments

Comments
 (0)