10000 generate resdata in process_value when possible, add test · matplotlib/matplotlib@a898d0c · GitHub
[go: up one dir, main page]

Skip to content

Commit a898d0c

Browse files
author
Nathan Goldbaum
committed
generate resdata in process_value when possible, add test
1 parent 063bcac commit a898d0c

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lib/matplotlib/colors.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,11 @@ def process_value(value):
907907
if np.issubdtype(dtype, np.integer) or dtype.type is np.bool_:
908908
# bool_/int8/int16 -> float32; int32/int64 -> float64
909909
dtype = np.promote_types(dtype, np.float32)
910+
# use np.asarray so data passed in as an ndarray subclass are
911+
# interpreted as an ndarray. See issue #6622.
910912
result = np.ma.array(value, dtype=dtype, copy=True)
911-
return result, is_scalar
913+
result_data = np.asarray(result.data)
914+
return result, result_data, is_scalar
912915

913916
def __call__(self, value, clip=None):
914917
"""
@@ -921,12 +924,12 @@ def __call__(self, value, clip=None):
921924
if clip is None:
922925
clip = self.clip
923926

924-
result, is_scalar = self.process_value(value)
927+
result, resdat, is_scalar = self.process_value(value)
925928

926929
self.autoscale_None(result)
927930
# Convert at least to float, without losing precision.
928-
(vmin,), _ = self.process_value(self.vmin)
929-
(vmax,), _ = self.process_value(self.vmax)
931+
(vmin,), _, _ = self.process_value(self.vmin)
932+
(vmax,), _, _ = self.process_value(self.vmax)
930933
if vmin == vmax:
931934
result.fill(0) # Or should it be all masked? Or 0.5?
932935
elif vmin > vmax:
@@ -937,9 +940,6 @@ def __call__(self, value, clip=None):
937940
result = np.ma.array(np.clip(result.filled(vmax), vmin, vmax),
938941
mask=mask)
939942
# ma division is very slow; we can take a shortcut
940-
# use np.asarray so data passed in as an ndarray subclass are
941-
# interpreted as an ndarray. See issue #6622.
942-
resdat = np.asarray(result.data)
943943
resdat -= vmin
944944
resdat /= (vmax - vmin)
945945
result = np.ma.array(resdat, mask=result.mask, copy=False)
@@ -955,8 +955,8 @@ def __call__(self, value, clip=None):
955955
def inverse(self, value):
956956
if not self.scaled():
957957
raise ValueError("Not invertible until scaled")
958-
(vmin,), _ = self.process_value(self.vmin)
959-
(vmax,), _ = self.process_value(self.vmax)
958+
(vmin,), _, _ = self.process_value(self.vmin)
959+
(vmax,), _, _ = self.process_value(self.vmax)
960960

961961
if cbook.iterable(value):
962962
val = np.ma.asarray(value)
@@ -991,7 +991,7 @@ def __call__(self, value, clip=None):
991991
if clip is None:
992992
clip = self.clip
993993

994-
result, is_scalar = self.process_value(value)
994+
result, _, is_scalar = self.process_value(value)
995995

996996
result = np.ma.masked_less_equal(result, 0, copy=False)
997997

@@ -1090,7 +1090,7 @@ def __call__(self, value, clip=None):
10901090
if clip is None:
10911091
clip = self.clip
10921092

1093-
result, is_scalar = self.process_value(value)
1093+
result, resdat, is_scalar = self.process_value(value)
10941094
self.autoscale_None(result)
10951095
vmin, vmax = self.vmin, self.vmax
10961096

@@ -1104,7 +1104,7 @@ def __call__(self, value, clip=None):
11041104
result = np.ma.array(np.clip(result.filled(vmax), vmin, vmax),
11051105
mask=mask)
11061106
# in-place equivalent of above can be much faster
1107-
resdat = self._transform(np.asarray(result.data))
1107+
resdat = self._transform(resdat)
11081108
resdat -= self._lower
11091109
resdat /= (self._upper - self._lower)
11101110

@@ -1183,7 +1183,7 @@ def __call__(self, value, clip=None):
11831183
if clip is None:
11841184
clip = self.clip
11851185

1186-
result, is_scalar = self.process_value(value)
1186+
result, _, is_scalar = self.process_value(value)
11871187

11881188
self.autoscale_None(result)
11891189
gamma = self.gamma
@@ -1300,7 +1300,7 @@ def __call__(self, value, clip=None):
13001300
if clip is None:
13011301
clip = self.clip
13021302

1303-
xx, is_scalar = self.process_value(value)
1303+
xx, _, is_scalar = self.process_value(value)
13041304
mask = np.ma.getmaskarray(xx)
13051305
xx = np.atleast_1d(xx.filled(self.vmax + 1))
13061306
if clip:

lib/matplotlib/tests/test_colors.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,23 @@ def test_tableau_order():
689689
'#bcbd22', '#17becf']
690690

691691
assert list(mcolors.TABLEAU_COLORS.values()) == dflt_cycle
692+
693+
694+
def test_ndarray_subclass_norm():
695+
# Emulate an ndarray subclass that handles units
696+
# which objects when adding or subtracting with other
697+
# arrays. See #6622 and #8696
698+
class MyArray(np.ndarray):
699+
def __isub__(self, other):
700+
raise RuntimeError
701+
702+
def __add__(self, other):
703+
raise RuntimeError
704+
705+
data = np.arange(-10, 10, 1, dtype=float)
706+
data = data.view(MyArray)
707+
708+
for norm in [mcolors.Normalize(), mcolors.LogNorm(),
709+
mcolors.SymLogNorm(3, vmax=5, linscale=1),
710+
mcolors.PowerNorm(1)]:
711+
assert_array_equal(norm(data.view(MyArray)), norm(data))

0 commit comments

Comments
 (0)
0