Skip to content

Commit b3c5bf0

Browse files
committed
Accept mask and bg keyworks to get_rgb/imshow.
1 parent dfbde8c commit b3c5bf0

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

spectral/algorithms/spymath.py

Lines changed: 4 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, ignore=None):
83+
def get_histogram_cdf_points(data, cdf_vals, ignore=None, mask=None):
8484
'''Returns input values corresponding to the data's CDF values.
8585
8686
Arguments:
@@ -108,11 +108,13 @@ def get_histogram_cdf_points(data, cdf_vals, ignore=None):
108108
associated CDF values in `cdf_vals`.
109109
'''
110110
data = data.ravel()
111+
if mask is not None:
112+
data = data[mask.ravel() != 0]
111113
if ignore is not None and ignore in data:
112114
data = data[np.where(data != ignore)]
113115
isort = np.argsort(data)
114116
N = len(data)
115-
return[data[isort[int(x * (N - 1))]] for x in cdf_vals]
117+
return [data[isort[int(x * (N - 1))]] for x in cdf_vals]
116118

117119

118120
class NaNValueWarning(UserWarning):

spectral/graphics/graphics.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ def get_rgb(source, bands=None, **kwargs):
556556
'''
557557
return get_rgb_meta(source, bands, **kwargs)[0]
558558

559+
def _fill_mask(arr, mask, fill_value):
560+
if mask is None:
561+
return arr
562+
arr[mask == 0] = np.array(fill_value) / 255.
563+
return arr
564+
559565
def get_rgb_meta(source, bands=None, **kwargs):
560566
'''Same as get_rgb but also returns some metadata.
561567
@@ -579,6 +585,8 @@ def get_rgb_meta(source, bands=None, **kwargs):
579585

580586
meta = {}
581587
monochrome = False
588+
mask = kwargs.get('mask', None)
589+
bg = kwargs.get('bg', settings.imshow_background_color)
582590

583591
if isinstance(source, Image) and len(source.shape) == 3:
584592
# Figure out which bands to display
@@ -637,7 +645,7 @@ def get_rgb_meta(source, bands=None, **kwargs):
637645
rgb = rgb.astype(int)
638646
pal = kwargs["colors"]
639647
rgb = pal[rgb[:,:,0]] / 255.
640-
return (rgb, meta)
648+
return (_fill_mask(rgb, mask, bg), meta)
641649
elif color_scale is not None:
642650
# Colors should be generated from the supplied color scale
643651
# This section assumes rgb colors in the range 0-255.
@@ -648,7 +656,7 @@ def get_rgb_meta(source, bands=None, **kwargs):
648656
rgb3 = np.zeros((s[0], s[1], 3), int)
649657
rgb3 = np.apply_along_axis(scale, 2, rgb)
650658
rgb = rgb3.astype(np.float) / 255.
651-
return (rgb, meta)
659+
return (_fill_mask(rgb, mask, bg), meta)
652660
else:
653661
# Only one band of data to display but still need to determine how
654662
# to scale the data values
@@ -732,11 +740,11 @@ def get_rgb_meta(source, bands=None, **kwargs):
732740
rgb[:, :, i] = 0
733741
else:
734742
rgb[:, :, i] = np.clip((rgb[:, :, i] - lower) / span, 0, 1)
735-
return (rgb, meta)
743+
return (_fill_mask(rgb, mask, bg), meta)
736744

737745
# For checking if valid keywords were supplied
738746
_get_rgb_kwargs = ('stretch', 'stretch_all', 'bounds', 'colors', 'color_scale',
739-
'auto_scale', 'ignore')
747+
'auto_scale', 'ignore', 'mask', 'bg')
740748

741749
def running_ipython():
742750
'''Returns True if ipython is running.'''

spectral/graphics/spypylab.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,8 @@ def plot(data, source=None):
12801280

12811281
set_mpl_interactive()
12821282

1283-
if source is not None and hasattr(source, 'bands'):
1283+
if source is not None and hasattr(source, 'bands') and \
1284+
source.bands.centers is not None:
12841285
xvals = source.bands.centers
12851286
else:
12861287
xvals = list(range(data.shape[-1]))

spectral/spectral.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class SpySettings:
7070
Width and height (in inches) of windows opened with `imshow`. If
7171
this value is `None`, matplotlib's default size is used.
7272
73+
`imshow_background_color` (3-tuple of integers, default (0,0,0)):
74+
75+
Default color to use for masked pixels in `imshow` displays.
76+
7377
`imshow_interpolation` (str, default `None`):
7478
7579
Pixel interpolation to be used in imshow windows. If this value
@@ -136,6 +140,7 @@ class SpySettings:
136140

137141
# imshow settings
138142
imshow_figure_size = None
143+
imshow_background_color = (0, 0, 0)
139144
imshow_interpolation = None
140145
imshow_stretch = (0.0, 1.0)
141146
imshow_stretch_all = True

0 commit comments

Comments
 (0)