Skip to content

Commit a7babbd

Browse files
authored
Merge pull request #2 from norwegian-geotechnical-institute/fix/tests
Fix/tests
2 parents b82b507 + 4ebdc18 commit a7babbd

3 files changed

Lines changed: 109 additions & 44 deletions

File tree

mpldxf/__init__.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +0,0 @@
1-
"""A matplotlib backend to enable writing matplotlib figures to DXF format.
2-
3-
DXF is a format that can be read by computer aided design (CAD) software.
4-
5-
This package builds on the ``ezdxf`` package writen by Manfred Moitzi:
6-
7-
http://bitbucket.org/mozman/ezdxf
8-
9-
Copyright (C) 2014 David M Kent
10-
11-
Permission is hereby granted, free of charge, to any person obtaining a copy of
12-
this software and associated documentation files (the "Software"), to deal in
13-
the Software without restriction, including without limitation the rights to
14-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
15-
the Software, and to permit persons to whom the Software is furnished to do so,
16-
subject to the following conditions:
17-
18-
The above copyright notice and this permission notice shall be included in all
19-
copies or substantial portions of the Software.
20-
21-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
23-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
24-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27-
"""
28-
import mpldxf.backend_dxf
29-
from .backend_dxf import FigureCanvasDxf
30-
31-
32-
__version__ = '0.1.0'
33-
34-
from ._version import get_versions
35-
__version__ = get_versions()['version']
36-
del get_versions

mpldxf/backend_dxf.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,10 @@ def _draw_mpl_lwpoly(self, gc, path, transform, obj):
181181

182182
else:
183183
if isinstance(vertices[0][0], float or np.float64):
184-
if vertices[0][0] != 0:
185-
entity = self.modelspace.add_lwpolyline(
186-
points=vertices, close=False, dxfattribs=dxfattribs
187-
) # set close to false because it broke some arrows
188-
else:
189-
entity = None
184+
entity = self.modelspace.add_lwpolyline(
185+
points=vertices, close=False, dxfattribs=dxfattribs
186+
) # set close to false because it broke some arrows
187+
190188
else:
191189
entity = [
192190
self.modelspace.add_lwpolyline(
@@ -203,8 +201,8 @@ def _draw_mpl_patch(self, gc, path, transform, rgbFace=None):
203201
"""Draw a matplotlib patch object"""
204202

205203
poly = self._draw_mpl_lwpoly(gc, path, transform, obj="patch")
206-
if math.isclose(path.vertices[0][1], 421.2598):
207-
pass
204+
if not poly:
205+
return
208206
# check to see if the patch is filled
209207
if rgbFace is not None:
210208
if type(poly) == list:

tests/test_backend_ezdxf.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Test the dxf matplotlib backend.
2+
3+
Copyright (C) 2014 David M Kent
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
"""
22+
23+
import unittest
24+
25+
import ezdxf
26+
import matplotlib
27+
from matplotlib import pyplot as plt
28+
import numpy as np
29+
from numpy.random import random
30+
31+
from mpldxf import backend_dxf
32+
33+
34+
matplotlib.backend_bases.register_backend("dxf", backend_dxf.FigureCanvas)
35+
36+
37+
class DxfBackendTestCase(unittest.TestCase):
38+
"""Tests for the dxf backend."""
39+
40+
def test_plot_line_with_no_axis(self):
41+
"""Test a simple line-plot command."""
42+
plt.gca().patch.set_visible(False)
43+
plt.plot(range(5), [1, 2, 3, 2, 4])
44+
plt.axis("off")
45+
46+
outfile = "tests/files/test_plot_line_with_no_axis.dxf"
47+
plt.close()
48+
49+
# Load the DXF file and inspect its content
50+
doc = ezdxf.readfile(outfile)
51+
modelspace = doc.modelspace()
52+
entities = list(modelspace)
53+
assert len(entities) == 2 # 1 line and the bounding box of the plot
54+
55+
def test_plot_line(self):
56+
"""Test a simple line-plot command."""
57+
plt.gca().patch.set_visible(False)
58+
plt.plot(range(3), [1, 2, 3])
59+
outfile = "tests/files/test_plot_line.dxf"
60+
plt.savefig(outfile, transparent=True)
61+
plt.close()
62+
63+
# Load the DXF file and inspect its content
64+
doc = ezdxf.readfile(outfile)
65+
modelspace = doc.modelspace()
66+
entities = list(modelspace)
67+
entity_types = set([entity.dxftype() for entity in entities])
68+
assert entity_types == {"LWPOLYLINE", "TEXT"}
69+
70+
def test_boxplot(self):
71+
"""Test a box-plot."""
72+
data = [
73+
[1, 2, 5, 6, 7, 8, 10, 11],
74+
[3, 4, 6, 7, 8, 9, 12, 13],
75+
[2, 4, 5, 6, 8, 10, 11, 12],
76+
[3, 5, 6, 7, 9, 10, 12, 13],
77+
]
78+
plt.boxplot(data)
79+
outfile = "tests/files/test_boxplot.dxf"
80+
plt.savefig(outfile)
81+
plt.close()
82+
83+
def test_contour(self):
84+
"""Test some contours."""
85+
x = np.linspace(-5.0, 5.0, 30)
86+
y = np.linspace(-5.0, 5.0, 30)
87+
X, Y = np.meshgrid(x, y)
88+
Z = np.sin(np.sqrt(X**2 + Y**2))
89+
plt.contour(X, Y, Z)
90+
outfile = "tests/files/test_contour.dxf"
91+
plt.savefig(outfile)
92+
plt.close()
93+
94+
def test_contourf(self):
95+
"""Test some filled contours."""
96+
x = np.linspace(-5.0, 5.0, 30)
97+
y = np.linspace(-5.0, 5.0, 30)
98+
X, Y = np.meshgrid(x, y)
99+
Z = np.sin(np.sqrt(X**2 + Y**2))
100+
plt.contourf(X, Y, Z)
101+
outfile = "tests/files/test_contourf.dxf"
102+
plt.savefig(outfile)
103+
plt.close()

0 commit comments

Comments
 (0)