Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: tri: prevent Triangulation from modifying specified input #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/animation/old_animation/movie_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
# This is a quick and dirty check; it leaves some spurious output
# for the user to puzzle over.
except OSError:
print not_found_msg
print(not_found_msg)
sys.exit("quitting\n")


Expand Down
34 changes: 28 additions & 6 deletions examples/api/custom_projection_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import matplotlib
from matplotlib.axes import Axes
from matplotlib.patches import Circle
from matplotlib.path import Path
Expand Down Expand Up @@ -385,9 +386,10 @@ class HammerTransform(Transform):
output_dims = 2
is_separable = False

def transform(self, ll):
def transform_non_affine(self, ll):
"""
Override the transform method to implement the custom transform.
Override the transform_non_affine method to implement the custom
transform.

The input and output are Nx2 numpy arrays.
"""
Expand All @@ -411,10 +413,25 @@ def transform(self, ll):
# differently-sized array, any transform that requires
# changing the length of the data array must happen within
# ``transform_path``.
def transform_path(self, path):
vertices = path.vertices
def transform_path_non_affine(self, path):
ipath = path.interpolated(path._interpolation_steps)
return Path(self.transform(ipath.vertices), ipath.codes)
transform_path_non_affine.__doc__ = \
Transform.transform_path_non_affine.__doc__

if matplotlib.__version__ < '1.2':
# Note: For compatibility with matplotlib v1.1 and older, you'll
# need to explicitly implement a ``transform`` method as well.
# Otherwise a ``NotImplementedError`` will be raised. This isn't
# necessary for v1.2 and newer, however.
transform = transform_non_affine

# Similarly, we need to explicitly override ``transform_path`` if
# compatibility with older matplotlib versions is needed. With v1.2
# and newer, only overriding the ``transform_path_non_affine``
# method is sufficient.
transform_path = transform_path_non_affine
transform_path.__doc__ = Transform.transform_path.__doc__

def inverted(self):
return HammerAxes.InvertedHammerTransform()
Expand All @@ -425,7 +442,7 @@ class InvertedHammerTransform(Transform):
output_dims = 2
is_separable = False

def transform(self, xy):
def transform_non_affine(self, xy):
x = xy[:, 0:1]
y = xy[:, 1:2]

Expand All @@ -435,7 +452,12 @@ def transform(self, xy):
longitude = 2 * np.arctan((z*x) / (2.0 * (2.0*z*z - 1.0)))
latitude = np.arcsin(y*z)
return np.concatenate((longitude, latitude), 1)
transform.__doc__ = Transform.transform.__doc__
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__

# As before, we need to implement the "transform" method for
# compatibility with matplotlib v1.1 and older.
if matplotlib.__version__ < '1.2':
transform = transform_non_affine

def inverted(self):
# The inverse of the inverse is the original transform... ;)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def _get_key(self, event):
[gdk.MOD1_MASK, 'alt'],
[gdk.CONTROL_MASK, 'ctrl'],):
if event.state & key_mask:
key = '{}+{}'.format(prefix, key)
key = '{0}+{1}'.format(prefix, key)

return key

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def _get_key(self, event):
]
for key_mask, prefix in modifiers:
if event.state & key_mask:
key = '{}+{}'.format(prefix, key)
key = '{0}+{1}'.format(prefix, key)

return key

Expand Down
14 changes: 13 additions & 1 deletion lib/matplotlib/backends/backend_gtk3agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,20 @@ def on_draw_event(self, widget, ctx):
return False

def blit(self, bbox=None):
# If bbox is None, blit the entire canvas to gtk. Otherwise
# blit only the area defined by the bbox.
if bbox is None:
bbox = self.figure.bbox

allocation = self.get_allocation()
w, h = allocation.width, allocation.height
x = int(bbox.x0)
y = h - int(bbox.y1)
width = int(bbox.x1) - int(bbox.x0)
height = int(bbox.y1) - int(bbox.y0)

self._bbox_queue.append(bbox)
self.queue_draw()
self.queue_draw_area(x, y, width, height)

def print_png(self, filename, *args, **kwargs):
# Do this so we can save the resolution of figure in the PNG file
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def _get_key( self, event ):
# prepend the ctrl, alt, super keys if appropriate (sorted in that order)
for modifier, prefix, Qt_key in self._modifier_keys:
if event.key() != Qt_key and int(event.modifiers()) & modifier == modifier:
key = u'{}+{}'.format(prefix, key)
key = u'{0}+{1}'.format(prefix, key)

return key

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def _get_key(self, event):
# note, shift is not added to the keys as this is already accounted for
for bitmask, prefix, key_name in modifiers:
if event.state & (1 << bitmask) and key_name not in key:
key = '{}+{}'.format(prefix, key)
key = '{0}+{1}'.format(prefix, key)

return key

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ def _get_key(self, evt):
[evt.AltDown, 'alt'],
[evt.ControlDown, 'ctrl'], ):
if meth():
key = '{}+{}'.format(prefix, key)
key = '{0}+{1}'.format(prefix, key)

return key

Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_triangulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,12 @@ def test_tripcolor():
plt.subplot(122)
plt.tripcolor(triang, facecolors=Cfaces, edgecolors='k')
plt.title('facecolors')

def test_no_modify():
triangles = np.array([[3, 2, 0], [3, 1, 0]], dtype=np.int32)
points = np.array([(0, 0), (0, 1.1), (1, 0), (1, 1)])

old_triangles = triangles.copy()
tri = mtri.Triangulation(points[:,0], points[:,1], triangles)
edges = tri.edges
assert_array_equal(old_triangles, triangles)
5 changes: 3 additions & 2 deletions lib/matplotlib/tri/triangulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ def __init__(self, x, y, triangles=None, mask=None):
neighbors = np.asarray(dt.triangle_neighbors, dtype=np.int32)
self._neighbors = np.roll(neighbors, 1, axis=1)
else:
# Triangulation specified.
self.triangles = np.asarray(triangles, dtype=np.int32)
# Triangulation specified. Copy, since we may correct triangle
# orientation.
self.triangles = np.array(triangles, dtype=np.int32)
if self.triangles.ndim != 2 or self.triangles.shape[1] != 3:
raise ValueError('triangles must be a (?,3) array')
if self.triangles.max() >= len(self.x):
Expand Down