Skip to content

Commit dc5a21a

Browse files
committed
FIX(PLOT.TC): TC was always testing PNG, ...
+ retorfitted to try all available formats. + list of forbidden formats based on my failres
1 parent a2de9ef commit dc5a21a

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

graphkit/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ def plot(self, filename=None, show=False,
176176
"""
177177
:param str filename:
178178
Write diagram into a file.
179-
The extension must be one of: ``.png .dot .jpg .jpeg .pdf .svg``
180-
Prefer ``.pdf`` or ``.svg`` to see solution-values in tooltips.
179+
Common extensions are ``.png .dot .jpg .jpeg .pdf .svg``
180+
call :func:`network.supported_plot_formats()` for more.
181181
:param boolean show:
182182
If it evaluates to true, opens the diagram in a matplotlib window.
183183
:param inputs:

graphkit/network.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ def plot(self, filename=None, show=False,
383383
384384
:param str filename:
385385
Write diagram into a file.
386-
The extension must be one of: ``.png .dot .jpg .jpeg .pdf .svg``
387-
Prefer ``.pdf`` or ``.svg`` to see solution-values in tooltips.
386+
Common extensions are ``.png .dot .jpg .jpeg .pdf .svg``
387+
call :func:`network.supported_plot_formats()` for more.
388388
:param boolean show:
389389
If it evaluates to true, opens the diagram in a matplotlib window.
390390
:param inputs:
@@ -454,15 +454,10 @@ def get_data_node(name, graph):
454454
return None
455455

456456

457-
def supported_plot_writers():
458-
return {
459-
".png": lambda gplot: gplot.create_png(),
460-
".dot": lambda gplot: gplot.to_string(),
461-
".jpg": lambda gplot: gplot.create_jpeg(),
462-
".jpeg": lambda gplot: gplot.create_jpeg(),
463-
".pdf": lambda gplot: gplot.create_pdf(),
464-
".svg": lambda gplot: gplot.create_svg(),
465-
}
457+
def supported_plot_formats():
458+
import pydot
459+
460+
return [".%s" % f for f in pydot.Dot().formats]
466461

467462

468463
def plot_graph(graph, filename=None, show=False, steps=None,
@@ -496,8 +491,8 @@ def plot_graph(graph, filename=None, show=False, steps=None,
496491
what to plot
497492
:param str filename:
498493
Write diagram into a file.
499-
The extension must be one of: ``.png .dot .jpg .jpeg .pdf .svg``
500-
Prefer ``.pdf`` or ``.svg`` to see solution-values in tooltips.
494+
Common extensions are ``.png .dot .jpg .jpeg .pdf .svg``
495+
call :func:`network.supported_plot_formats()` for more.
501496
:param boolean show:
502497
If it evaluates to true, opens the diagram in a matplotlib window.
503498
:param steps:
@@ -603,16 +598,15 @@ def get_node_name(a):
603598

604599
# save plot
605600
if filename:
601+
formats = supported_plot_formats()
606602
_basename, ext = os.path.splitext(filename)
607-
writers = supported_plot_writers()
608-
plot_writer = supported_plot_writers().get(ext.lower())
609-
if not plot_writer:
603+
if not ext.lower() in formats:
610604
raise ValueError(
611605
"Unknown file format for saving graph: %s"
612606
" File extensions must be one of: %s"
613-
% (ext, ' '.join(writers)))
614-
with open(filename, "wb") as fh:
615-
fh.write(plot_writer(g))
607+
% (ext, " ".join(formats)))
608+
609+
g.write(filename, format=ext.lower()[1:])
616610

617611
# display graph via matplotlib
618612
if show:

test/test_graphkit.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,41 @@ def test_plotting():
116116
inputs = {'a': 1, 'b1': 2}
117117
solution=pipeline(inputs)
118118

119-
for ext in network.supported_plot_writers():
120-
tdir = tempfile.mkdtemp(suffix=ext)
121-
png_file = osp.join(tdir, "workflow.png")
122-
pipeline.plot(png_file, inputs=inputs, solution=solution, outputs=['asked', 'b1'])
123-
try:
124-
assert osp.exists(png_file)
125-
finally:
126-
shutil.rmtree(tdir, ignore_errors=True)
119+
# ...not working on my PC ...
120+
forbidden_formats = ".dia .hpgl .mif .pcl .pic .vtx .xlib".split()
121+
tdir = tempfile.mkdtemp()
122+
counter = 0
123+
try:
124+
for ext in network.supported_plot_formats():
125+
if ext in forbidden_formats:
126+
continue
127+
128+
counter += 1
129+
fpath = osp.join(tdir, "workflow-%i%s" % (counter, ext))
130+
pipeline.plot(fpath, inputs=inputs, solution=solution, outputs=['asked', 'b1'])
131+
assert osp.exists(fpath)
132+
133+
counter += 1
134+
fpath = osp.join(tdir, "workflow-%i%s" % (counter, ext))
135+
pipeline.plot(fpath)
136+
assert osp.exists(fpath)
137+
finally:
138+
shutil.rmtree(tdir, ignore_errors=True)
139+
127140
try:
128141
pipeline.plot('bad.format')
129142
assert False, "Should had failed writting arbitrary file format!"
130143
except ValueError as ex:
131144
assert "Unknown file format" in str(ex)
132145

133146
## Check help msg lists all siupported formats
134-
for ext in network.supported_plot_writers():
147+
for ext in network.supported_plot_formats():
135148
assert ext in str(ex)
136149

137150

138151
def test_plotting_docstring():
139-
for ext in network.supported_plot_writers():
152+
common_formats = ".png .dot .jpg .jpeg .pdf .svg".split()
153+
for ext in common_formats:
140154
assert ext in network.plot_graph.__doc__
141155

142156

0 commit comments

Comments
 (0)