@@ -706,30 +706,24 @@ def __call__(self, X, alpha=None, bytes=False):
706
706
if not self ._isinit :
707
707
self ._init ()
708
708
709
- # Take the bad mask from a masked array, or in all other cases defer
710
- # np.isnan() to after we have converted to an array.
711
- mask_bad = X .mask if np .ma .is_masked (X ) else None
712
709
xa = np .array (X , copy = True )
713
- if mask_bad is None :
714
- mask_bad = np .isnan (xa )
715
710
if not xa .dtype .isnative :
716
711
xa = xa .byteswap ().newbyteorder () # Native byteorder is faster.
717
712
if xa .dtype .kind == "f" :
718
713
xa *= self .N
719
- # Negative values are out of range, but astype(int) would
720
- # truncate them towards zero.
721
- xa [xa < 0 ] = - 1
722
714
# xa == 1 (== N after multiplication) is not out of range.
723
715
xa [xa == self .N ] = self .N - 1
724
- # Avoid converting large positive values to negative integers.
725
- np .clip (xa , - 1 , self .N , out = xa )
716
+ # Pre-compute the masks before casting to int (which can truncate
717
+ # negative values to zero or wrap large floats to negative ints).
718
+ mask_under = xa < 0
719
+ mask_over = xa >= self .N
720
+ # If input was masked, get the bad mask from it; else mask out nans.
721
+ mask_bad = X .mask if np .ma .is_masked (X ) else np .isnan (xa )
726
722
with np .errstate (invalid = "ignore" ):
727
723
# We need this cast for unsigned ints as well as floats
728
724
xa = xa .astype (int )
729
- # Set the over-range indices before the under-range;
730
- # otherwise the under-range values get converted to over-range.
731
- xa [xa > self .N - 1 ] = self ._i_over
732
- xa [xa < 0 ] = self ._i_under
725
+ xa [mask_under ] = self ._i_under
726
+ xa [mask_over ] = self ._i_over
733
727
xa [mask_bad ] = self ._i_bad
734
728
735
729
lut = self ._lut
@@ -747,13 +741,9 @@ def __call__(self, X, alpha=None, bytes=False):
747
741
f"alpha is array-like but its shape { alpha .shape } does "
748
742
f"not match that of X { xa .shape } " )
749
743
rgba [..., - 1 ] = alpha
750
-
751
744
# If the "bad" color is all zeros, then ignore alpha input.
752
- if (lut [- 1 ] == 0 ).all () and np .any (mask_bad ):
753
- if np .iterable (mask_bad ) and mask_bad .shape == xa .shape :
754
- rgba [mask_bad ] = (0 , 0 , 0 , 0 )
755
- else :
756
- rgba [..., :] = (0 , 0 , 0 , 0 )
745
+ if (lut [- 1 ] == 0 ).all ():
746
+ rgba [mask_bad ] = (0 , 0 , 0 , 0 )
757
747
758
748
if not np .iterable (X ):
759
749
rgba = tuple (rgba )
0 commit comments