From d8841a86957949b5414c7288861bf83f49cf6979 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 14 Nov 2016 21:40:26 -0800 Subject: [PATCH 1/5] add matrix checking function for quiver input --- lib/matplotlib/cbook.py | 15 ++++++++++++++- lib/matplotlib/quiver.py | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 0e8c0f50e150..3cd4adf5c49b 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -1014,6 +1014,7 @@ def mkdirs(newdir, mode=0o777): if exception.errno != errno.EEXIST: raise + class GetRealpathAndStat(object): def __init__(self): self._cache = {} @@ -1734,7 +1735,7 @@ def recursive_remove(path): os.removedirs(fname) else: os.remove(fname) - #os.removedirs(path) + # os.removedirs(path) else: os.remove(path) @@ -2701,3 +2702,15 @@ def __exit__(self, exc_type, exc_value, traceback): os.rmdir(path) except OSError: pass + + +def is_matrix(obj): + ''' + This is a test for whether the input is a matrix. + If the input is a matrix, raise an error. Otherwise, + return the object as it is. + ''' + cast_result = np.asanyarray(obj) + if type(cast_result) == np.matrix: + raise ValueError("The input cannot be matrix") + return obj diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 594ca8744ef2..6a30960b2a8c 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -422,6 +422,16 @@ def __init__(self, ax, *args, **kw): """ self.ax = ax X, Y, U, V, C = _parse_args(*args) + if X: + cbook.is_matrix(X) + if Y: + cbook.is_matrix(Y) + if U: + cbook.is_matrix(U) + if V: + cbook.is_matrix(V) + if C: + cbook.is_matrix(C) self.X = X self.Y = Y self.XY = np.hstack((X[:, np.newaxis], Y[:, np.newaxis])) From bba1ea6569be6e1c2c5ce31ea931519f51ed3562 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 14 Nov 2016 23:49:38 -0800 Subject: [PATCH 2/5] remove the if statement and use isinstance function --- lib/matplotlib/cbook.py | 2 +- lib/matplotlib/quiver.py | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 3cd4adf5c49b..230e57f15b40 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2711,6 +2711,6 @@ def is_matrix(obj): return the object as it is. ''' cast_result = np.asanyarray(obj) - if type(cast_result) == np.matrix: + if isinstance(cast_result, np.matrix): raise ValueError("The input cannot be matrix") return obj diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 6a30960b2a8c..42c9181c134c 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -422,16 +422,11 @@ def __init__(self, ax, *args, **kw): """ self.ax = ax X, Y, U, V, C = _parse_args(*args) - if X: - cbook.is_matrix(X) - if Y: - cbook.is_matrix(Y) - if U: - cbook.is_matrix(U) - if V: - cbook.is_matrix(V) - if C: - cbook.is_matrix(C) + cbook.is_matrix(X) + cbook.is_matrix(Y) + cbook.is_matrix(U) + cbook.is_matrix(V) + cbook.is_matrix(C) self.X = X self.Y = Y self.XY = np.hstack((X[:, np.newaxis], Y[:, np.newaxis])) From 4bdcac3fc58eef8c435c647fb9849dfad18be6a6 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Wed, 16 Nov 2016 20:52:42 -0800 Subject: [PATCH 3/5] change doc string and return type in check_matrix --- lib/matplotlib/cbook.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 230e57f15b40..6e998e4a2834 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2704,13 +2704,17 @@ def __exit__(self, exc_type, exc_value, traceback): pass -def is_matrix(obj): - ''' - This is a test for whether the input is a matrix. - If the input is a matrix, raise an error. Otherwise, - return the object as it is. - ''' - cast_result = np.asanyarray(obj) - if isinstance(cast_result, np.matrix): +def check_array(obj): + """ + Helper function to check whether the input is an array or + can be casted to an array type object. + + If it is not an array or cannot be casted to an array + type obejct, it will raise an error to notify users that + they should use array. Otherwise, it will return the casted + array type object. + """ + cast_obj = np.asanyarray(obj) + if isinstance(cast_obj, np.matrix): raise ValueError("The input cannot be matrix") - return obj + return cast_obj From 22655b69675885c3fa004c5e75aad950e249835e Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Tue, 22 Nov 2016 11:55:30 -0800 Subject: [PATCH 4/5] move checking into _parse_args --- lib/matplotlib/cbook.py | 2 +- lib/matplotlib/quiver.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 6e998e4a2834..22048b408e6e 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2704,7 +2704,7 @@ def __exit__(self, exc_type, exc_value, traceback): pass -def check_array(obj): +def _check_array(obj): """ Helper function to check whether the input is an array or can be casted to an array type object. diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 42c9181c134c..cc73e091a560 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -389,6 +389,11 @@ def _parse_args(*args): else: indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) X, Y = [np.ravel(a) for a in indexgrid] + cbook._check_array(X) + cbook._check_array(Y) + cbook._check_array(U) + cbook._check_array(V) + cbook._check_array(C) return X, Y, U, V, C @@ -422,11 +427,6 @@ def __init__(self, ax, *args, **kw): """ self.ax = ax X, Y, U, V, C = _parse_args(*args) - cbook.is_matrix(X) - cbook.is_matrix(Y) - cbook.is_matrix(U) - cbook.is_matrix(V) - cbook.is_matrix(C) self.X = X self.Y = Y self.XY = np.hstack((X[:, np.newaxis], Y[:, np.newaxis])) From 73a69a2d36a947fa76b94f98988fff0d3c5b6a92 Mon Sep 17 00:00:00 2001 From: Jun Tan Date: Mon, 28 Nov 2016 22:57:43 -0800 Subject: [PATCH 5/5] refactor _parse_ags --- lib/matplotlib/quiver.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index cc73e091a560..0f0472ae7923 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -372,12 +372,13 @@ def _parse_args(*args): X, Y, U, V, C = [None] * 5 args = list(args) - # The use of atleast_1d allows for handling scalar arguments while also - # keeping masked arrays - if len(args) == 3 or len(args) == 5: + if len(args) == 2 or len(args) == 4: + V = np.atleast_1d(args.pop(-1)) + U = np.atleast_1d(args.pop(-1)) + elif len(args) == 3 or len(args) == 5: C = np.atleast_1d(args.pop(-1)) - V = np.atleast_1d(args.pop(-1)) - U = np.atleast_1d(args.pop(-1)) + V = np.atleast_1d(args.pop(-1)) + U = np.atleast_1d(args.pop(-1)) if U.ndim == 1: nr, nc = 1, U.shape[0] else: @@ -389,8 +390,6 @@ def _parse_args(*args): else: indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) X, Y = [np.ravel(a) for a in indexgrid] - cbook._check_array(X) - cbook._check_array(Y) cbook._check_array(U) cbook._check_array(V) cbook._check_array(C)