makeRGBA/ImageItem: Applying alpha mask on numpy.nan data values (#406)
* Applying alpha mask on numpy.nan data values * Typesafe, checking for `data.dtype.kind`
This commit is contained in:
parent
96a4270a30
commit
071e429535
@ -78,7 +78,7 @@ def siScale(x, minVal=1e-25, allowUnicode=True):
|
|||||||
pref = SI_PREFIXES_ASCII[m+8]
|
pref = SI_PREFIXES_ASCII[m+8]
|
||||||
p = .001**m
|
p = .001**m
|
||||||
|
|
||||||
return (p, pref)
|
return (p, pref)
|
||||||
|
|
||||||
|
|
||||||
def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, allowUnicode=True):
|
def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, allowUnicode=True):
|
||||||
@ -1035,7 +1035,6 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
|
|||||||
============== ==================================================================================
|
============== ==================================================================================
|
||||||
"""
|
"""
|
||||||
profile = debug.Profiler()
|
profile = debug.Profiler()
|
||||||
|
|
||||||
if data.ndim not in (2, 3):
|
if data.ndim not in (2, 3):
|
||||||
raise TypeError("data must be 2D or 3D")
|
raise TypeError("data must be 2D or 3D")
|
||||||
if data.ndim == 3 and data.shape[2] > 4:
|
if data.ndim == 3 and data.shape[2] > 4:
|
||||||
@ -1083,7 +1082,12 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
|
|||||||
dtype = np.ubyte
|
dtype = np.ubyte
|
||||||
else:
|
else:
|
||||||
dtype = np.min_scalar_type(lut.shape[0]-1)
|
dtype = np.min_scalar_type(lut.shape[0]-1)
|
||||||
|
|
||||||
|
# awkward, but fastest numpy native nan evaluation
|
||||||
|
#
|
||||||
|
nanMask = None
|
||||||
|
if data.dtype.kind == 'f' and np.isnan(data.min()):
|
||||||
|
nanMask = np.isnan(data)
|
||||||
# Apply levels if given
|
# Apply levels if given
|
||||||
if levels is not None:
|
if levels is not None:
|
||||||
if isinstance(levels, np.ndarray) and levels.ndim == 2:
|
if isinstance(levels, np.ndarray) and levels.ndim == 2:
|
||||||
@ -1106,10 +1110,8 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
|
|||||||
if minVal == maxVal:
|
if minVal == maxVal:
|
||||||
maxVal = np.nextafter(maxVal, 2*maxVal)
|
maxVal = np.nextafter(maxVal, 2*maxVal)
|
||||||
data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=dtype)
|
data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=dtype)
|
||||||
|
|
||||||
|
|
||||||
profile()
|
profile()
|
||||||
|
|
||||||
# apply LUT if given
|
# apply LUT if given
|
||||||
if lut is not None:
|
if lut is not None:
|
||||||
data = applyLookupTable(data, lut)
|
data = applyLookupTable(data, lut)
|
||||||
@ -1152,7 +1154,12 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False):
|
|||||||
imgData[..., 3] = 255
|
imgData[..., 3] = 255
|
||||||
else:
|
else:
|
||||||
alpha = True
|
alpha = True
|
||||||
|
|
||||||
|
# apply nan mask through alpha channel
|
||||||
|
if nanMask is not None:
|
||||||
|
alpha = True
|
||||||
|
imgData[nanMask, 3] = 0
|
||||||
|
|
||||||
profile()
|
profile()
|
||||||
return imgData, alpha
|
return imgData, alpha
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user