Skip to content

Commit 24681a1

Browse files
committed
Accept "ignore" keyword for bad/non-data values in rendering functions.
If the "ignore" keyword value is specified, the corresponding value will not be used for calculating RGB limits for default color limits or user-specified histogram stretches. This is useful when bad pixels are assigned a unique data value. * algorithsm/spymath.py: (get_histogram_cdf_points): Accept "ignore" keyword. * graphics/graphics.py: (get_rgb_meta): Pass "ignore" ignore keyword on to `get_histogram_cdf_points`. _get_rgb_kwargs: Add "ignore" to list of accepted keywords.
1 parent 4ff45d8 commit 24681a1

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

spectral/algorithms/spymath.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def matrix_sqrt(X=None, symmetric=False, inverse=False, eigs=None):
8080
return V.dot(SRV).dot(np.linalg.inv(V))
8181

8282

83-
def get_histogram_cdf_points(data, cdf_vals):
83+
def get_histogram_cdf_points(data, cdf_vals, ignore=None):
8484
'''Returns input values corresponding to the data's CDF values.
8585
8686
Arguments:
@@ -96,16 +96,24 @@ def get_histogram_cdf_points(data, cdf_vals):
9696
example, to get the values of `data` corresponding to the 1% lower
9797
tail and 5% upper tail, this argument would be (0.01, 0.95).
9898
99+
`ignore` (numeric, default `None`):
100+
101+
A scalar value that should be ignored when computing histogram
102+
points (e.g., a value that indicates bad data). If this valus is
103+
not specified, all data are used.
104+
99105
Return value:
100106
101107
A list specifying the values in `data` that correspond to the
102108
associated CDF values in `cdf_vals`.
103109
'''
104110
data = data.ravel()
111+
if ignore is not None and ignore in data:
112+
data = data[np.where(data != ignore)]
105113
isort = np.argsort(data)
106114
N = len(data)
107115
return[data[isort[int(x * (N - 1))]] for x in cdf_vals]
108-
116+
109117

110118
class NaNValueWarning(UserWarning):
111119
pass

spectral/graphics/graphics.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,29 +687,35 @@ def get_rgb_meta(source, bands=None, **kwargs):
687687
if stretch.shape not in ((2,), (3, 2)):
688688
raise ValueError("`stretch` keyword must be numeric or a " \
689689
"sequence with shape (2,) or (3, 2).")
690+
nondata = kwargs.get('ignore', None)
690691
if stretch.ndim == 1:
691692
if monochrome:
692-
s = get_histogram_cdf_points(rgb[:, :, 0], stretch)
693+
s = get_histogram_cdf_points(rgb[:, :, 0], stretch,
694+
ignore=nondata)
693695
rgb_lims = [s, s, s]
694696
elif stretch_all:
695697
# Stretch each color component independently
696-
rgb_lims = [get_histogram_cdf_points(rgb[:, :, i], stretch) \
698+
rgb_lims = [get_histogram_cdf_points(rgb[:, :, i], stretch,
699+
ignore=nondata) \
697700
for i in range(3)]
698701
else:
699702
# Use a common lower/upper limit for each band by taking
700703
# the lowest lower limit and greatest upper limit.
701-
lims = np.array([get_histogram_cdf_points(rgb[:,:,i], stretch) \
704+
lims = np.array([get_histogram_cdf_points(rgb[:,:,i], stretch,
705+
ignore=nondata) \
702706
for i in range(3)])
703707
minmax = np.array([lims[:,0].min(), lims[:,1].max()])
704708
rgb_lims = minmax[np.newaxis, :].repeat(3, axis=0)
705709
else:
706710
if monochrome:
707711
# Not sure why anyone would want separate RGB stretches for
708712
# a gray-scale image but we'll let them.
709-
rgb_lims = [get_histogram_cdf_points(rgb[:,:,0], stretch[i]) \
713+
rgb_lims = [get_histogram_cdf_points(rgb[:,:,0], stretch[i],
714+
ignore=nondata) \
710715
for i in range(3)]
711716
elif stretch_all:
712-
rgb_lims = [get_histogram_cdf_points(rgb[:,:,i], stretch[i]) \
717+
rgb_lims = [get_histogram_cdf_points(rgb[:,:,i], stretch[i],
718+
ignore=nondata) \
713719
for i in range(3)]
714720
else:
715721
msg = 'Can not use common stretch if different stretch ' \
@@ -730,7 +736,7 @@ def get_rgb_meta(source, bands=None, **kwargs):
730736

731737
# For checking if valid keywords were supplied
732738
_get_rgb_kwargs = ('stretch', 'stretch_all', 'bounds', 'colors', 'color_scale',
733-
'auto_scale')
739+
'auto_scale', 'ignore')
734740

735741
def running_ipython():
736742
'''Returns True if ipython is running.'''

0 commit comments

Comments
 (0)