[Python-modules-commits] [python-mplexporter] 87/135: Increase test coverage
Wolfgang Borgert
debacle at moszumanska.debian.org
Tue Sep 23 21:19:07 UTC 2014
This is an automated email from the git hooks/post-receive script.
debacle pushed a commit to branch master
in repository python-mplexporter.
commit 69ec174160896768e0040dc5dc3a6a46a23c72d5
Author: Jake Vanderplas <vanderplas at astro.washington.edu>
Date: Mon Mar 10 16:33:13 2014 -0700
Increase test coverage
---
mplexporter/renderers/__init__.py | 2 +-
mplexporter/renderers/example_renderer.py | 29 ------
mplexporter/renderers/fake_renderer.py | 62 +++++++++++
mplexporter/renderers/vega_renderer.py | 13 ++-
mplexporter/tests/test_basic.py | 167 +++++++++++++++++++++++++++---
notebooks/VegaTest.ipynb | 12 +--
6 files changed, 228 insertions(+), 57 deletions(-)
diff --git a/mplexporter/renderers/__init__.py b/mplexporter/renderers/__init__.py
index 5353156..ba85b1a 100644
--- a/mplexporter/renderers/__init__.py
+++ b/mplexporter/renderers/__init__.py
@@ -9,4 +9,4 @@ abstract base class
from .base import Renderer
from .vega_renderer import VegaRenderer, fig_to_vega
from .vincent_renderer import VincentRenderer, fig_to_vincent
-from .example_renderer import ExampleRenderer
+from .fake_renderer import FakeRenderer, FullFakeRenderer
diff --git a/mplexporter/renderers/example_renderer.py b/mplexporter/renderers/example_renderer.py
deleted file mode 100644
index 92549cf..0000000
--- a/mplexporter/renderers/example_renderer.py
+++ /dev/null
@@ -1,29 +0,0 @@
-"""
-Example Renderer
-================
-This shows an example of a do-nothing renderer, along with how to use it.
-"""
-from .base import Renderer
-
-
-class ExampleRenderer(Renderer):
- def __init__(self):
- self.output = ""
-
- def open_figure(self, fig, props):
- self.output += "opening figure\n"
-
- def close_figure(self, fig):
- self.output += "closing figure\n"
-
- def open_axes(self, ax, props):
- self.output += " opening axes\n"
-
- def close_axes(self, ax):
- self.output += " closing axes\n"
-
- def draw_line(self, data, coordinates, style, mplobj=None):
- self.output += " draw line with {0} points\n".format(data.shape[0])
-
- def draw_markers(self, data, coordinates, style, mplobj=None):
- self.output += " draw {0} markers\n".format(data.shape[0])
diff --git a/mplexporter/renderers/fake_renderer.py b/mplexporter/renderers/fake_renderer.py
new file mode 100644
index 0000000..80fccf1
--- /dev/null
+++ b/mplexporter/renderers/fake_renderer.py
@@ -0,0 +1,62 @@
+from .base import Renderer
+
+
+class FakeRenderer(Renderer):
+ """
+ Fake Renderer
+
+ This is a fake renderer which simply outputs a text tree representing the
+ elements found in the plot(s). This is used in the unit tests for the
+ package.
+
+ Below are the methods your renderer must implement. You are free to do
+ anything you wish within the renderer (i.e. build an XML or JSON
+ representation, call an external API, etc.) Here the renderer just
+ builds a simple string representation for testing purposes.
+ """
+ def __init__(self):
+ self.output = ""
+
+ def open_figure(self, fig, props):
+ self.output += "opening figure\n"
+
+ def close_figure(self, fig):
+ self.output += "closing figure\n"
+
+ def open_axes(self, ax, props):
+ self.output += " opening axes\n"
+
+ def close_axes(self, ax):
+ self.output += " closing axes\n"
+
+ def draw_text(self, text, position, coordinates, style,
+ text_type=None, mplobj=None):
+ self.output += " draw text '{0}' {1}\n".format(text, text_type)
+
+ def draw_path(self, data, coordinates, pathcodes, style,
+ offset=None, offset_coordinates="data", mplobj=None):
+ self.output += " draw path with {0} vertices\n".format(data.shape[0])
+
+ def draw_image(self, imdata, extent, coordinates, style, mplobj=None):
+ self.output += " draw image of size {0}\n".format(len(imdata))
+
+
+class FullFakeRenderer(FakeRenderer):
+ """
+ Renderer with the full complement of methods.
+
+ When the following are left undefined, they will be implemented via
+ other methods in the class. They can be defined explicitly for
+ more efficient or specialized use within the renderer implementation.
+ """
+ def draw_line(self, data, coordinates, style, mplobj=None):
+ self.output += " draw line with {0} points\n".format(data.shape[0])
+
+ def draw_markers(self, data, coordinates, style, mplobj=None):
+ self.output += " draw {0} markers\n".format(data.shape[0])
+
+ def draw_path_collection(self, paths, path_coordinates, path_transforms,
+ offsets, offset_coordinates, offset_order,
+ styles, mplobj=None):
+ self.output += (" draw path collection "
+ "with {0} offsets\n".format(offsets.shape[0]))
diff --git a/mplexporter/renderers/vega_renderer.py b/mplexporter/renderers/vega_renderer.py
index 0ba4677..c8d5040 100644
--- a/mplexporter/renderers/vega_renderer.py
+++ b/mplexporter/renderers/vega_renderer.py
@@ -18,10 +18,8 @@ class VegaRenderer(Renderer):
def open_axes(self, ax, props):
if len(self.axes) > 0:
warnings.warn("multiple axes not yet supported")
- self.axes = [dict(type="x", scale="x", ticks=10,
- title=props['xlabel']),
- dict(type="y", scale="y", ticks=10,
- title=props['ylabel'])]
+ self.axes = [dict(type="x", scale="x", ticks=10),
+ dict(type="y", scale="y", ticks=10)]
self.scales = [dict(name="x",
domain=props['xlim'],
type="linear",
@@ -79,6 +77,13 @@ class VegaRenderer(Renderer):
}
})
+ def draw_text(self, text, position, coordinates, style,
+ text_type=None, mplobj=None):
+ if text_type == 'xlabel':
+ self.axes[0]['title'] = text
+ elif text_type == 'ylabel':
+ self.axes[1]['title'] = text
+
class VegaHTML(object):
def __init__(self, renderer):
diff --git a/mplexporter/tests/test_basic.py b/mplexporter/tests/test_basic.py
index dbc3247..82f98eb 100644
--- a/mplexporter/tests/test_basic.py
+++ b/mplexporter/tests/test_basic.py
@@ -1,30 +1,163 @@
from ..exporter import Exporter
-from ..renderers import ExampleRenderer
+from ..renderers import FakeRenderer, FullFakeRenderer
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
+import numpy as np
-FAKE_OUTPUT = """
-opening figure
- opening axes
- draw line with 20 points
- draw 10 markers
- closing axes
-closing figure
-"""
+def fake_renderer_output(fig, Renderer):
+ renderer = Renderer()
+ exporter = Exporter(renderer)
+ exporter.run(fig)
+ return renderer.output
+
+
+def _assert_output_equal(text1, text2):
+ print text1
+ for line1, line2 in zip(text1.strip().split(), text2.strip().split()):
+ assert line1 == line2
-def test_fakerenderer():
+
+def test_lines():
fig, ax = plt.subplots()
ax.plot(range(20), '-k')
- ax.plot(range(10), '.k')
- renderer = ExampleRenderer()
- exporter = Exporter(renderer)
- exporter.run(fig)
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw path with 20 vertices
+ closing axes
+ closing figure
+ """)
- for line1, line2 in zip(renderer.output.strip().split(),
- FAKE_OUTPUT.strip().split()):
- assert line1 == line2
+ _assert_output_equal(fake_renderer_output(fig, FullFakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw line with 20 points
+ closing axes
+ closing figure
+ """)
+
+
+def test_markers():
+ fig, ax = plt.subplots()
+ ax.plot(range(2), 'ok')
+
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw path with 25 vertices
+ draw path with 25 vertices
+ closing axes
+ closing figure
+ """)
+
+ _assert_output_equal(fake_renderer_output(fig, FullFakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw 2 markers
+ closing axes
+ closing figure
+ """)
+
+
+def test_path_collection():
+ fig, ax = plt.subplots()
+ ax.scatter(range(3), range(3))
+
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw path with 25 vertices
+ draw path with 25 vertices
+ draw path with 25 vertices
+ closing axes
+ closing figure
+ """)
+
+ _assert_output_equal(fake_renderer_output(fig, FullFakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw path collection with 3 offsets
+ closing axes
+ closing figure
+ """)
+
+
+def test_text():
+ fig, ax = plt.subplots()
+ ax.set_xlabel("my x label")
+ ax.set_ylabel("my y label")
+ ax.set_title("my title")
+ ax.text(0.5, 0.5, "my text")
+
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw text 'my text' None
+ draw text 'my x label' xlabel
+ draw text 'my y label' ylabel
+ draw text 'my title' title
+ closing axes
+ closing figure
+ """)
+
+
+def test_path():
+ fig, ax = plt.subplots()
+ ax.add_patch(plt.Circle((0, 0), 1))
+ ax.add_patch(plt.Rectangle((0, 0), 1, 2))
+
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw path with 25 vertices
+ draw path with 4 vertices
+ closing axes
+ closing figure
+ """)
+
+
+def test_multiaxes():
+ fig, ax = plt.subplots(2)
+ ax[0].plot(range(4))
+ ax[1].plot(range(10))
+
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw path with 4 vertices
+ closing axes
+ opening axes
+ draw path with 10 vertices
+ closing axes
+ closing figure
+ """)
+
+
+def test_image():
+ np.random.seed(0) # image size depends on the seed
+ fig, ax = plt.subplots()
+ ax.imshow(np.random.random((10, 10)),
+ cmap=plt.cm.jet, interpolation='nearest')
+
+ _assert_output_equal(fake_renderer_output(fig, FakeRenderer),
+ """
+ opening figure
+ opening axes
+ draw image of size 2848
+ closing axes
+ closing figure
+ """)
diff --git a/notebooks/VegaTest.ipynb b/notebooks/VegaTest.ipynb
index fb02f7e..c07a069 100644
--- a/notebooks/VegaTest.ipynb
+++ b/notebooks/VegaTest.ipynb
@@ -77,7 +77,7 @@
"metadata": {},
"output_type": "display_data",
"text": [
- "<IPython.core.display.HTML at 0x10258be10>"
+ "<IPython.core.display.HTML at 0x101d64390>"
]
}
],
@@ -106,7 +106,7 @@
"outputs": [
{
"html": [
- "<div id=\"vis37347\"></div><script>\n",
+ "<div id=\"vis10381\"></div><script>\n",
"\n",
"( function() {\n",
" var _do_plot = function() {\n",
@@ -115,7 +115,7 @@
" return;\n",
" }\n",
" vg.parse.spec({\"width\": 480, \"scales\": [{\"range\": \"width\", \"type\": \"linear\", \"domain\": [0.0, 10.0], \"name\": \"x\"}, {\"range\": \"height\", \"type\": \"linear\", \"domain\": [-1.3999999999999999, 1.3999999999999999], \"name\": \"y\"}], \"marks\": [{\"from\": {\"data\": \"table001\"}, \"type\": \"line\", \"properties\": {\"enter\": {\"strokeWidth\": {\"value\": 3}, \"strokeOpacity\": {\"value\": 0.5}, \"stroke\": {\"value\": \"#FF0000\"}, \"interpolate\": {\"v [...]
- " chart({el: \"#vis37347\"}).update();\n",
+ " chart({el: \"#vis10381\"}).update();\n",
" });\n",
" };\n",
" _do_plot();\n",
@@ -126,7 +126,7 @@
"output_type": "pyout",
"prompt_number": 3,
"text": [
- "<mplexporter.renderers.vega_renderer.VegaHTML at 0x10258bbd0>"
+ "<mplexporter.renderers.vega_renderer.VegaHTML at 0x101d21550>"
]
}
],
@@ -153,7 +153,7 @@
"output_type": "stream",
"stream": "stdout",
"text": [
- "<div id=\"vis43350\"></div><script>\n",
+ "<div id=\"vis41930\"></div><script>\n",
"\n",
"( function() {\n",
" var _do_plot = function() {\n",
@@ -162,7 +162,7 @@
" return;\n",
" }\n",
" vg.parse.spec({\"width\": 480, \"scales\": [{\"range\": \"width\", \"type\": \"linear\", \"domain\": [0.0, 10.0], \"name\": \"x\"}, {\"range\": \"height\", \"type\": \"linear\", \"domain\": [-1.3999999999999999, 1.3999999999999999], \"name\": \"y\"}], \"marks\": [{\"from\": {\"data\": \"table001\"}, \"type\": \"line\", \"properties\": {\"enter\": {\"strokeWidth\": {\"value\": 3}, \"strokeOpacity\": {\"value\": 0.5}, \"stroke\": {\"value\": \"#FF0000\"}, \"interpolate\": {\"v [...]
- " chart({el: \"#vis43350\"}).update();\n",
+ " chart({el: \"#vis41930\"}).update();\n",
" });\n",
" };\n",
" _do_plot();\n",
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-mplexporter.git
More information about the Python-modules-commits
mailing list