Skip to content

Commit 3c5acf7

Browse files
PEP-8 compliance and more
tests for multi-view.
1 parent e68ce6d commit 3c5acf7

File tree

6 files changed

+59
-25
lines changed

6 files changed

+59
-25
lines changed

matplotview/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Optional, Iterable, Type, Union
22
from matplotlib.artist import Artist
33
from matplotlib.axes import Axes
4-
from matplotview._view_axes import view_wrapper, ViewSpecification, DEFAULT_RENDER_DEPTH
4+
from matplotview._view_axes import view_wrapper, ViewSpecification
5+
56

67
__all__ = ["view", "inset_zoom_axes", "ViewSpecification"]
78

9+
810
def view(
911
axes: Axes,
1012
axes_to_view: Axes,
@@ -67,7 +69,7 @@ def inset_zoom_axes(
6769
render_depth: Optional[int] = None,
6870
filter_set: Optional[Iterable[Union[Type[Artist], Artist]]] = None,
6971
scale_lines: bool = True,
70-
transform = None,
72+
transform=None,
7173
zorder: int = 5,
7274
**kwargs
7375
) -> Axes:

matplotview/_transform_renderer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def draw_gouraud_triangle(self, gc, points, colors, transform):
234234
IdentityTransform())
235235

236236
# Images prove to be especially messy to deal with...
237-
def draw_image(self, gc, x, y, im, transform = None):
237+
def draw_image(self, gc, x, y, im, transform=None):
238238
mag = self.get_image_magnification()
239239
shift_data_transform = self._get_transfer_transform(
240240
IdentityTransform()
@@ -292,4 +292,3 @@ def draw_image(self, gc, x, y, im, transform = None):
292292
self.__renderer.draw_image(gc, x, y, out_arr, None)
293293
else:
294294
self.__renderer.draw_image(gc, x, y, out_arr)
295-

matplotview/_view_axes.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22
import itertools
3-
from typing import Type, List, Optional, Callable, Any, Set, Dict, Union
3+
from typing import Type, List, Optional, Any, Set, Dict, Union
44
from matplotlib.axes import Axes
55
from matplotlib.transforms import Bbox
66
import matplotlib.docstring as docstring
@@ -11,6 +11,7 @@
1111

1212
DEFAULT_RENDER_DEPTH = 5
1313

14+
1415
class _BoundRendererArtist:
1516
"""
1617
Provides a temporary wrapper around a given artist, inheriting its
@@ -85,6 +86,7 @@ def do_3d_projection(self) -> float:
8586

8687
return res
8788

89+
8890
def _view_from_pickle(builder, args):
8991
"""
9092
PRIVATE: Construct a View wrapper axes given an axes builder and class.
@@ -106,13 +108,15 @@ def __post_init__(self):
106108
self.filter_set = set(self.filter_set)
107109
self.scale_lines = bool(self.scale_lines)
108110

111+
109112
class __ViewType:
110113
"""
111114
PRIVATE: A simple identifier class for identifying view types, a view
112115
will inherit from the axes class it is wrapping and this type...
113116
"""
114117
...
115118

119+
116120
# Cache classes so grabbing the same type twice leads to actually getting the
117121
# same type (and type comparisons work).
118122
@functools.lru_cache(None)
@@ -201,6 +205,14 @@ def get_children(self) -> List[Artist]:
201205
# renderer, and therefore to the correct location.
202206
child_list = super().get_children()
203207

208+
def filter_check(artist, filter_set):
209+
if(filter_set is None):
210+
return True
211+
return (
212+
(artist not in filter_set)
213+
and (type(artist) not in filter_set)
214+
)
215+
204216
if(self.__renderer is not None):
205217
for ax, spec in self.view_specifications.items():
206218
mock_renderer = _TransformRenderer(
@@ -219,11 +231,7 @@ def get_children(self) -> List[Artist]:
219231
for a in itertools.chain(
220232
ax._children,
221233
ax.child_axes
222-
) if(
223-
(spec.filter_set is None)
224-
or ((a not in spec.filter_set)
225-
and (type(a) not in spec.filter_set))
226-
)
234+
) if(filter_check(a, spec.filter_set))
227235
])
228236

229237
return child_list
@@ -367,4 +375,4 @@ def from_axes(
367375
View.__name__ = f"{View.__name__}[{axes_class.__name__}]"
368376
View.__qualname__ = f"{View.__qualname__}[{axes_class.__name__}]"
369377

370-
return View
378+
return View

matplotview/tests/test_view_obj.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import matplotlib.pyplot as plt
22
from matplotlib.testing.decorators import check_figures_equal
33
from matplotview.tests.utils import plotting_test, matches_post_pickle
4-
from matplotview import view, view_wrapper, inset_zoom_axes, \
5-
DEFAULT_RENDER_DEPTH, ViewSpecification
4+
from matplotview import view, inset_zoom_axes, ViewSpecification
5+
from matplotview._view_axes import DEFAULT_RENDER_DEPTH, view_wrapper
66
import numpy as np
77

88

@@ -38,7 +38,7 @@ def test_getters_and_setters(fig_test, fig_ref):
3838
assert ax1 in ax3.view_specifications
3939
assert ax3.view_specifications[ax1].image_interpolation == "nearest"
4040
assert ax3.get_max_render_depth() == DEFAULT_RENDER_DEPTH
41-
assert ax3.view_specifications[ax1].scale_lines == True
41+
assert ax3.view_specifications[ax1].scale_lines is True
4242
assert ax3.view_specifications[ax1].filter_set is None
4343

4444
# Attempt setting to different values...
@@ -62,6 +62,7 @@ def test_getters_and_setters(fig_test, fig_ref):
6262
ax3.set_xlim(0, 30)
6363
ax3.set_ylim(0, 30)
6464

65+
6566
@plotting_test()
6667
def test_subplot_view_pickle(fig_test):
6768
np.random.seed(1)
@@ -74,14 +75,15 @@ def test_subplot_view_pickle(fig_test):
7475
ax_test1.add_patch(plt.Circle((3, 3), 1, ec="black", fc="blue"))
7576
ax_test1.text(10, 10, "Hello World!", size=14)
7677
ax_test1.imshow(im_data, origin="lower", cmap="Blues", alpha=0.5,
77-
interpolation="nearest")
78+
interpolation="nearest")
7879
ax_test2 = view(ax_test2, ax_test1)
7980
ax_test2.set_aspect(ax_test1.get_aspect())
8081
ax_test2.set_xlim(ax_test1.get_xlim())
8182
ax_test2.set_ylim(ax_test1.get_ylim())
8283

8384
assert matches_post_pickle(fig_test)
8485

86+
8587
@plotting_test()
8688
def test_zoom_plot_pickle(fig_test):
8789
np.random.seed(1)
@@ -106,6 +108,7 @@ def test_zoom_plot_pickle(fig_test):
106108

107109
assert matches_post_pickle(fig_test)
108110

111+
109112
@plotting_test()
110113
def test_3d_view_pickle(fig_test):
111114
X = Y = np.arange(-5, 5, 0.25)
@@ -122,4 +125,28 @@ def test_3d_view_pickle(fig_test):
122125
ax2_test.set_ylim(-10, 10)
123126
ax2_test.set_zlim(-2, 2)
124127

125-
assert matches_post_pickle(fig_test)
128+
assert matches_post_pickle(fig_test)
129+
130+
131+
@plotting_test()
132+
def test_multiplot_pickle(fig_test):
133+
ax_test1, ax_test2, ax_test3 = fig_test.subplots(1, 3)
134+
135+
ax_test1.add_patch(plt.Circle((1, 1), 1.5, ec="black", fc=(0, 0, 1, 0.5)))
136+
ax_test3.add_patch(plt.Circle((3, 1), 1.5, ec="black", fc=(1, 0, 0, 0.5)))
137+
138+
for ax in (ax_test1, ax_test3):
139+
ax.set_aspect(1)
140+
ax.relim()
141+
ax.autoscale_view()
142+
143+
ax_test2 = view(
144+
view(ax_test2, ax_test1, scale_lines=False),
145+
ax_test3, scale_lines=False
146+
)
147+
148+
ax_test2.set_aspect(1)
149+
ax_test2.set_xlim(-0.5, 4.5)
150+
ax_test2.set_ylim(-0.5, 2.5)
151+
152+
assert matches_post_pickle(fig_test)

matplotview/tests/test_view_rendering.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from matplotlib.testing.decorators import check_figures_equal
44
from matplotview import view, inset_zoom_axes
55

6+
67
@check_figures_equal(tol=6)
78
def test_double_plot(fig_test, fig_ref):
89
np.random.seed(1)
@@ -15,7 +16,7 @@ def test_double_plot(fig_test, fig_ref):
1516
ax_test1.add_patch(plt.Circle((3, 3), 1, ec="black", fc="blue"))
1617
ax_test1.text(10, 10, "Hello World!", size=14)
1718
ax_test1.imshow(im_data, origin="lower", cmap="Blues", alpha=0.5,
18-
interpolation="nearest")
19+
interpolation="nearest")
1920
ax_test2 = view(ax_test2, ax_test1)
2021
ax_test2.set_aspect(ax_test1.get_aspect())
2122
ax_test2.set_xlim(ax_test1.get_xlim())
@@ -161,7 +162,9 @@ def test_polar_view(fig_test, fig_ref):
161162
def test_map_projection_view(fig_test, fig_ref):
162163
x = np.linspace(-2.5, 2.5, 20)
163164
y = np.linspace(-1, 1, 20)
164-
circ_gen = lambda: plt.Circle((1.5, 0.25), 0.7, ec="black", fc="blue")
165+
166+
def circ_gen():
167+
return plt.Circle((1.5, 0.25), 0.7, ec="black", fc="blue")
165168

166169
# Test case...
167170
ax_t1 = fig_test.add_subplot(1, 2, 1, projection="hammer")
@@ -215,4 +218,4 @@ def test_double_view(fig_test, fig_ref):
215218
for ax in (ax_test1, ax_test3, ax_ref1, ax_ref3):
216219
ax.set_aspect(1)
217220
ax.relim()
218-
ax.autoscale_view()
221+
ax.autoscale_view()

matplotview/tests/utils.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import functools
2-
31
import numpy as np
42
import matplotlib.pyplot as plt
53

@@ -23,7 +21,7 @@ def matches_post_pickle(figure):
2321
return np.all(img_expected == img_result)
2422

2523

26-
def plotting_test(num_figs = 1, *args, **kwargs):
24+
def plotting_test(num_figs=1, *args, **kwargs):
2725
def plotting_decorator(function):
2826
def test_plotting():
2927
plt.close("all")
@@ -36,6 +34,3 @@ def test_plotting():
3634
return test_plotting
3735

3836
return plotting_decorator
39-
40-
41-

0 commit comments

Comments
 (0)