From e7a5cf79788ab5da905da9f144df0cc4b744e76d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 22 Nov 2018 22:53:28 +0100 Subject: [PATCH] Simplify use of Path._fast_from_codes_and_verts. All uses of Path._fast_from_codes_and_verts create the internals dict by copying the attribute from another Path. Instead of having the caller create that dict, just let him pass a Path from which to copy the attributes. Note that this means that the copying will now also use the simplify_threshold from the previous Path; however I think it is clear that the previous behavior of not copying it was an oversight (compounded by the fact that simplify_threshold is rarely, if ever, overwritten on a per-Path basis). --- lib/matplotlib/path.py | 50 +++++++++++++++++------------------- lib/matplotlib/transforms.py | 12 +++------ 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 8035367540cd..4c9a3f69c324 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -157,35 +157,32 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1, self._readonly = False @classmethod - def _fast_from_codes_and_verts(cls, verts, codes, internals=None): + def _fast_from_codes_and_verts(cls, verts, codes, internals_from=None): """ - Creates a Path instance without the expense of calling the constructor + Creates a Path instance without the expense of calling the constructor. Parameters ---------- verts : numpy array codes : numpy array - internals : dict or None - The attributes that the resulting path should have. Allowed keys - are ``readonly``, ``should_simplify``, ``simplify_threshold``, and - ``interpolation_steps``. - + internals_from : Path or None + If not None, another `Path` from which the attributes + ``should_simplify``, ``simplify_threshold``, and + ``interpolation_steps`` will be copied. Note that ``readonly`` is + never copied, and always set to ``False`` by this constructor. """ - internals = internals or {} pth = cls.__new__(cls) pth._vertices = _to_unmasked_float_array(verts) pth._codes = codes - pth._readonly = internals.pop('readonly', False) - pth.should_simplify = internals.pop('should_simplify', True) - pth.simplify_threshold = ( - internals.pop('simplify_threshold', - rcParams['path.simplify_threshold']) - ) - pth._interpolation_steps = internals.pop('interpolation_steps', 1) - if internals: - raise ValueError('Unexpected internals provided to ' - '_fast_from_codes_and_verts: ' - '{0}'.format('\n *'.join(internals))) + pth._readonly = False + if internals_from is not None: + pth._should_simplify = internals_from._should_simplify + pth._simplify_threshold = internals_from._simplify_threshold + pth._interpolation_steps = internals_from._interpolation_steps + else: + pth._should_simplify = True + pth._simplify_threshold = rcParams['path.simplify_threshold'] + pth._interpolation_steps = 1 return pth def _update_values(self): @@ -436,14 +433,13 @@ def cleaned(self, transform=None, remove_nans=False, clip=None, ------- Path instance with cleaned up vertices and codes. """ - vertices, codes = _path.cleanup_path(self, transform, - remove_nans, clip, - snap, stroke_width, - simplify, curves, sketch) - internals = {'should_simplify': self.should_simplify and not simplify, - 'simplify_threshold': self.simplify_threshold, - 'interpolation_steps': self._interpolation_steps} - return Path._fast_from_codes_and_verts(vertices, codes, internals) + vertices, codes = _path.cleanup_path( + self, transform, remove_nans, clip, snap, stroke_width, simplify, + curves, sketch) + pth = Path._fast_from_codes_and_verts(vertices, codes, self) + if not simplify: + pth._should_simplify = False + return pth def transformed(self, transform): """ diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index af84319d5792..424876d74fb9 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -1552,9 +1552,7 @@ def transform_path_non_affine(self, path): ``transform_path_affine(transform_path_non_affine(values))``. """ x = self.transform_non_affine(path.vertices) - return Path._fast_from_codes_and_verts(x, path.codes, - {'interpolation_steps': path._interpolation_steps, - 'should_simplify': path.should_simplify}) + return Path._fast_from_codes_and_verts(x, path.codes, path) def transform_angles(self, angles, pts, radians=False, pushoff=1e-5): """ @@ -2762,9 +2760,7 @@ def _revalidate(self): self._transformed_points = \ Path._fast_from_codes_and_verts( self._transform.transform_non_affine(self._path.vertices), - None, - {'interpolation_steps': self._path._interpolation_steps, - 'should_simplify': self._path.should_simplify}) + None, self._path) self._invalid = 0 def get_transformed_points_and_affine(self): @@ -2832,9 +2828,7 @@ def _revalidate(self): self._transformed_points = \ Path._fast_from_codes_and_verts( self._transform.transform_non_affine(patch_path.vertices), - None, - {'interpolation_steps': patch_path._interpolation_steps, - 'should_simplify': patch_path.should_simplify}) + None, patch_path) self._invalid = 0