File: graphics.py

package info (click to toggle)
python-spectral 0.22.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 13,161; makefile: 7
file content (760 lines) | stat: -rw-r--r-- 26,752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
'''
Common functions for extracting and manipulating data for graphical display.
'''

from __future__ import absolute_import, division, print_function, unicode_literals

import io
from numbers import Number
import numpy as np
import sys
import time
import warnings

from ..algorithms.spymath import get_histogram_cdf_points
from ..config import spy_colors
from ..image import Image
from ..spectral import settings

class WindowProxy(object):
    '''Base class for proxy objects to access data from display windows.'''
    def __init__(self, window):
        self._window = window


class SpyWindow():
    def get_proxy(self):
        return WindowProxy(self)


def view(*args, **kwargs):
    '''
    Opens a window and displays a raster greyscale or color image.

    Usage::

        view(source, bands=None, **kwargs)

    Arguments:

        `source` (:class:`spectral.Image` or :class:`numpy.ndarray`):

            Source image data to display.  `source` can be and instance of a
            :class:`spectral.Image` (e.g., :class:`spectral.SpyFile` or
            :class:`spectral.ImageArray`) or a :class:`numpy.ndarray`. `source`
            must have shape `MxN` or `MxNxB`.

        `bands` (3-tuple of ints):

            Optional list of indices for bands to display in the red, green,
            and blue channels, respectively.

    Keyword Arguments:

        `stretch` (bool):

            If `stretch` evaluates True, the highest value in the data source
            will be scaled to maximum color channel intensity.

        `stretch_all` (bool):

            If `stretch_all` evaluates True, the highest value of the data
            source in each color channel will be set to maximum intensity.

        `bounds` (2-tuple of ints):

            Clips the input data at (lower, upper) values.

        `title` (str):

            Text to display in the new window frame.

    `source` is the data source and can be either a :class:`spectral.Image`
    object or a numpy array. If `source` has shape `MxN`, the image will be
    displayed in greyscale. If its shape is `MxNx3`, the three layers/bands
    will be displayed as the red, green, and blue components of the displayed
    image, respectively. If its shape is `MxNxB`, where `B > 3`, the first,
    middle, and last bands will be displayed in the RGB channels, unless
    `bands` is specified.
    '''
    from .rasterwindow import RasterWindow
    if not running_ipython():
        warn_no_ipython()
    check_wx_app()

    rgb = get_rgb(*args, **kwargs)

    # To plot pixel spectrum on double-click, create a reference
    # back to the original SpyFile object.
    if isinstance(args[0], Image):
        kwargs["data source"] = args[0]

    if "colors" not in kwargs:
        rgb = (rgb * 255).astype(np.uint8)
    else:
        rgb = rgb.astype(np.uint8)

    frame = RasterWindow(None, -1, rgb, **kwargs)
    frame.Raise()
    frame.Show()
    return frame.get_proxy()


def view_cube(data, *args, **kwargs):
    '''Renders an interactive 3D hypercube in a new window.

    Arguments:

        `data` (:class:`spectral.Image` or :class:`numpy.ndarray`):

            Source image data to display.  `data` can be and instance of a
            :class:`spectral.Image` (e.g., :class:`spectral.SpyFile` or
            :class:`spectral.ImageArray`) or a :class:`numpy.ndarray`. `source`
            must have shape `MxN` or `MxNxB`.

    Keyword Arguments:

        `bands` (3-tuple of ints):

            3-tuple specifying which bands from the image data should be
            displayed on top of the cube.

        `top` (:class:`numpy.ndarray` or :class:`PIL.Image`):

            Data to display on top of the cube. This will supercede the
            `bands` keyword.

        `scale` (:class:`spectral.ColorScale`)

            A color scale to be used for color in the sides of the cube. If
            this keyword is not specified,
            :obj:`spectral.graphics.colorscale.defaultColorScale` is used.

        `size` (2-tuple of ints):

            Width and height (in pixels) for initial size of the new window.

        `background` (3-tuple of floats):

            Background RGB color of the scene. Each value should be in the
            range [0, 1]. If not specified, the background will be black.

        `title` (str):

            Title text to display in the new window frame.

    This function opens a new window, renders a 3D hypercube, and accepts
    keyboard input to manipulate the view of the hypercube.  Accepted keyboard
    inputs are printed to the console output.  Focus must be on the 3D window
    to accept keyboard input.
    '''
    from .hypercube import HypercubeWindow

    if not running_ipython():
        warn_no_ipython()
    check_wx_app()

    window = HypercubeWindow(data, None, -1, *args, **kwargs)
    window.Show()
    window.Raise()
    return window.get_proxy()


def view_nd(data, *args, **kwargs):
    '''
    Creates a 3D window that displays ND data from an image.

    Arguments:

        `data` (:class:`spectral.ImageArray` or :class:`numpy.ndarray`):

            Source image data to display.  `data` can be and instance of a
            :class:`spectral.ImageArray or a :class:`numpy.ndarray`. `source`
            must have shape `MxNxB`, where M >= 3.

    Keyword Arguments:

        `classes` (:class:`numpy.ndarray`):

            2-dimensional array of integers specifying the classes of each
            pixel in `data`. `classes` must have the same dimensions as the
            first two dimensions of `data`.

        `features` (list or list of integer lists):

            This keyword specifies which bands/features from `data` should be
            displayed in the 3D window. It must be defined as one of the
            following:

            #. A length-3 list of integer feature IDs. In this case, the data
               points will be displayed in the positive x,y,z octant using
               features associated with the 3 integers.

            #. A length-6 list of integer feature IDs. In this case, each
               integer specifies a single feature index to be associated with
               the coordinate semi-axes x, y, z, -x, -y, and -z (in that
               order). Each octant will display data points using the features
               associated with the 3 semi-axes for that octant.

            #. A length-8 list of length-3 lists of integers. In this case,
               each length-3 list specfies the features to be displayed in a
               single octants (the same semi-axis can be associated with
               different features in different octants).  Octants are ordered
               starting with the postive x,y,z octant and procede
               counterclockwise around the z-axis, then procede similarly
               around the negative half of the z-axis.  An octant triplet can
               be specified as None instead of a list, in which case nothing
               will be rendered in that octant.

        `labels` (list):

            List of labels to be displayed next to the axis assigned to a
            feature.  If not specified, the feature index is shown by default.

            The `str()` function will be called on each item of the list so,
            for example, a list of wavelengths can be passed as the labels.

        `size` (2-tuple of ints)

            Specifies the initial size (pixel rows/cols) of the window.

        `title` (string)

            The title to display in the ND window title bar.

    Returns an NDWindowProxy object with a `classes` member to access the
    current class labels associated with data points and a `set_features`
    member to specify which features are displayed.
    '''
    from .ndwindow import NDWindow, validate_args
    if not running_ipython():
        warn_no_ipython()
    check_wx_app()

    validate_args(data, *args, **kwargs)
    window = NDWindow(data, None, -1, *args, **kwargs)
    window.Show()
    window.Raise()
    return window.get_proxy()


def view_indexed(*args, **kwargs):
    '''
    Opens a window and displays a raster image for the provided color map data.

    Usage::

        view_indexed(data, **kwargs)

    Arguments:

        `data` (:class:`numpy.ndarray`):

            An `MxN` array of integer values that correspond to colors in a
            color palette.

    Keyword Arguments:

        `colors` (list of 3-tuples of ints):

            This parameter provides an alternate color map to use for display.
            The parameter is a list of 3-tuples defining RGB values, where R,
            G, and B are in the range [0-255].

        `title` (str):

            Text to display in the new window frame.

    The default color palette used is defined by :obj:`spectral.spy_colors`.
    '''
    if not running_ipython():
        warn_no_ipython()
    check_wx_app()

    if 'colors' not in kwargs:
        kwargs['colors'] = spy_colors

    return view(*args, **kwargs)

def imshow(data, bands=None, **kwargs):
    '''A wrapper around matplotlib's imshow for multi-band images.

    Arguments:

        `data` (SpyFile or ndarray):

            Can have shape (R, C) or (R, C, B).

        `bands` (tuple of integers, optional)

            If `bands` has 3 values, the bands specified are extracted from
            `data` to be plotted as the red, green, and blue colors,
            respectively. If it contains a single value, then a single band
            will be extracted from the image.

    Keyword Arguments:

        show_xaxis (bool, default True):

            Indicates whether to display x-axis ticks and labels.

        show_yaxis (bool, default True):

            Indicates whether to display y-axis ticks and labels.

    This function is a wrapper around
    :func:`~spectral.graphics.graphics.get_rgb` and matplotlib's imshow.
    All keyword arguments other than those described above are passed on to
    the wrapped functions.

    This function defaults the color scale (imshow's "cmap" keyword) to
    "gray". To use imshow's default color scale, call this function with
    keyword `cmap=None`.
    '''
    import matplotlib.pyplot as plt
    show_xaxis = True
    show_yaxis = True
    if 'show_xaxis' in kwargs:
        show_xaxis = kwargs.pop('show_xaxis')
    if 'show_yaxis' in kwargs:
        show_yaxis = kwargs.pop('show_yaxis')

    rgb_kwargs = {}
    for k in ['stretch', 'stretch_all', 'bounds']:
        if k in kwargs:
            rgb_kwargs[k] = kwargs.pop(k)
    
    imshow_kwargs = {'cmap': 'gray'}
    imshow_kwargs.update(kwargs)

    rgb = get_rgb(data, bands, **rgb_kwargs)

    # Allow matplotlib.imshow to apply a color scale to single-band image.
    if len(data.shape) == 2:
        rgb = rgb[:, :, 0]

    ax = plt.imshow(rgb, **imshow_kwargs)
    if show_xaxis == False:
        plt.gca().xaxis.set_visible(False)
    if show_yaxis == False:
        plt.gca().yaxis.set_visible(False)
    return ax

def make_pil_image(*args, **kwargs):
    '''Creates a PIL Image object.

    USAGE: make_pil_image(source [, bands] [stretch=True] [stretch_all=False],
                          [bounds = (lower, upper)] )

    See `get_rgb` for description of arguments.
    '''
    try:
        from PIL import Image, ImageDraw
    except ImportError:
        import Image
        import ImageDraw

    rgb = get_rgb(*args, **kwargs)
    rgb = (rgb * 255).astype(np.ubyte)
    img = Image.fromarray(rgb)
    return img


def save_rgb(filename, data, bands=None, **kwargs):
    '''
    Saves a viewable image to a JPEG (or other format) file.

    Usage::

        save_rgb(filename, data, bands=None, **kwargs)

    Arguments:

        `filename` (str):

            Name of image file to save (e.g. "rgb.jpg")

        `data` (:class:`spectral.Image` or :class:`numpy.ndarray`):

            Source image data to display.  `data` can be and instance of a
            :class:`spectral.Image` (e.g., :class:`spectral.SpyFile` or
            :class:`spectral.ImageArray`) or a :class:`numpy.ndarray`. `data`
            must have shape `MxN` or `MxNxB`.  If thes shape is `MxN`, the
            image will be saved as greyscale (unless keyword `colors` is
            specified). If the shape is `MxNx3`, it will be interpreted as
            three `MxN` images defining the R, G, and B channels respectively.
            If `B > 3`, the first, middle, and last images in `data` will be
            used, unless `bands` is specified.

        `bands` (3-tuple of ints):

            Optional list of indices for bands to use in the red, green,
            and blue channels, respectively.

    Keyword Arguments:

        `format` (str):

            The image file format to create.  Must be a format recognized by
            :mod:`PIL` (e.g., 'png', 'tiff', 'bmp').  If `format` is not
            provided, 'jpg' is assumed.


        See :func:`~spectral.graphics.graphics.get_rgb` for descriptions of
        additional keyword arguments.

    Examples:

        Save a color view of an image by specifying RGB band indices::

            save_image('rgb.jpg', img, [29, 19, 9]])

        Save the same image as **png**::

            save_image('rgb.png', img, [29, 19, 9]], format='png')

        Save classification results using the default color palette (note that
        the color palette must be passed explicitly for `clMap` to be
        interpreted as a color map)::

            save_image('results.jpg', clMap, colors=spectral.spy_colors)
    '''
    kwargs = kwargs.copy()
    fmt = kwargs.pop('format', None)

    im = make_pil_image(*(data, bands), **kwargs)
    im.save(filename, fmt, quality=100)


def get_rgb(source, bands=None, **kwargs):
    '''Extract RGB data for display from a SpyFile object or numpy array.

    USAGE: rgb = get_rgb(source [, bands] [, stretch=<arg> | , bounds=<arg>]
                         [, stretch_all=<arg>])

    Arguments:

        `source` (:class:`spectral.SpyFile` or :class:`numpy.ndarray`):

            Data source from which to extract the RGB data.

        `bands` (list of `int`) (optional):

            Optional triplet of indices which specifies the bands to extract
            for the red, green, and blue components, respectively. If this
            arg is not given, SpyFile object, it's metadata dict will be
            checked to see if it contains a "default bands" item.  If it does
            not, then first, middle and last band will be returned.

    Keyword Arguments:

        `stretch` (numeric or tuple):

            This keyword specifies two points on the cumulative histogram of
            the input data for performing a linear stretch of RGB value for the
            data. Numeric values given for this parameter are expected to be
            between 0 and 1. This keyword can be expressed in three forms:

            1. As a 2-tuple. In this case the two values specify the lower and
               upper points of the cumulative histogram respectively. The
               specified stretch will be performed independently on each of the
               three color channels unless the `stretch_all` keyword is set to
               True, in which case all three color channels will be stretched
               identically.

            2. As a 3-tuple of 2-tuples. In this case, Each channel will be
               stretched according to its respective 2-tuple in the keyword
               argument.

            3. As a single numeric value. In this case, the value indicates the
               size of the histogram tail to be applied at both ends of the
               histogram for each color channel. `stretch=a` is equivalent to
               `stretch=(a, 1-a)`.

            If neither `stretch` nor `bounds` are specified, then the default
            value of `stretch` defined by `spectral.settings.imshow_stretch`
            will be used.
    
        `bounds` (tuple):

            This keyword functions similarly to the `stretch` keyword, except
            numeric values are in image data units instead of cumulative
            histogram values. The form of this keyword is the same as the first
            two forms for the `stretch` keyword (i.e., either a 2-tuple of
            numbers or a 3-tuple of 2-tuples of numbers).
    
        `stretch_all` (bool):

            If this keyword is True, each color channel will be scaled
            independently.

        `color_scale` (:class:`~spectral.graphics.colorscale.ColorScale`):

            A color scale to be applied to a single-band image.

        `auto_scale` (bool):

            If `color_scale` is provided and `auto_scale` is True, the min/max
            values of the color scale will be mapped to the min/max data
            values.

        `colors` (ndarray):

            If `source` is a single-band integer-valued np.ndarray and this
            keyword is provided, then elements of `source` are assumed to be
            color index values that specify RGB values in `colors`.

    Examples:

    Select color limits corresponding to 2% tails in the data histogram:

    >>> imshow(x, stretch=0.02)

    Same as above but specify upper and lower limits explicitly:

    >>> imshow(x, stretch=(0.02, 0.98))

    Same as above but specify limits for each RGB channel explicitly:

    >>> imshow(x, stretch=((0.02, 0.98), (0.02, 0.98), (0.02, 0.98)))
    '''
    return get_rgb_meta(source, bands, **kwargs)[0]

def _fill_mask(arr, mask, fill_value):
    if mask is None:
        return arr
    arr[mask == 0] = np.array(fill_value) / 255.
    return arr

def get_rgb_meta(source, bands=None, **kwargs):
    '''Same as get_rgb but also returns some metadata.

    Inputs are the same as for get_rgb but the return value is a 2-tuple whose
    first element is the get_rgb return array and whose second element is a
    dictionary containing some metadata values for the data RGB conversion.
    '''
    for k in kwargs:
        if k not in _get_rgb_kwargs:
            raise ValueError('Invalid keyword: {0}'.format(k))

    if bands is None:
        bands = []
    if len(bands) not in (0, 1, 3):
        raise Exception("Invalid number of bands specified.")

    meta = {}
    monochrome = False
    mask = kwargs.get('mask', None)
    bg = kwargs.get('bg', settings.imshow_background_color)

    if isinstance(source, Image) and len(source.shape) == 3:
        # Figure out which bands to display
        s = source.shape
        if len(bands) == 0:
            # No bands specified. What should we show?
            if hasattr(source, 'metadata') and \
              'default bands' in source.metadata:
                try:
                    bands = [int(b) for b in source.metadata['default bands']]
                except:
                    msg = 'Unable to interpret "default bands" in image ' \
                      'metadata. Defaulting to first, middle, & last band.'
                    warnings.warn(msg)
            elif source.shape[-1] == 1:
                bands = [0]
        if len(bands) == 0:
            # Pick the first, middle, and last bands
            n = source.shape[-1]
            bands = [0, n // 2, n - 1]
        rgb = source.read_bands(bands).astype(float)
        meta['bands'] = bands
    else:
        # It should be a numpy array
        if source.ndim == 2:
            source = source[:, :, np.newaxis]
        s = source.shape

        if s[2] == 1:
            if len(bands) == 0:
                bands = [0]
            elif np.max(bands) > 0:
                raise ValueError('Invalid band index for monochrome image.')
        if s[2] == 3 and len(bands) == 0:
            # Keep data as is.
            bands = [0, 1, 2]
        elif s[2] > 3 and len(bands) == 0:
            # More than 3 bands in data but no bands specified so take
            # first, middle, & last bands.
            bands = [0, s[2] / 2, s[2] - 1]

        rgb = np.take(source, bands, 2).astype(float)
        if rgb.ndim == 2:
            rgb = rgb[:, :, np.newaxis]
        meta['bands'] = bands

    color_scale = kwargs.get('color_scale', None)
    auto_scale = kwargs.get('auto_scale', False)

    # If it's either color-indexed or monochrome
    if rgb.shape[2] == 1:
        s = rgb.shape
        if "colors" in kwargs:
            # color-indexed image
            meta['mode'] = 'indexed'
            rgb = rgb.astype(int)
            pal = kwargs["colors"]
            rgb = pal[rgb[:,:,0]] / 255.
            return (_fill_mask(rgb, mask, bg), meta)
        elif color_scale is not None:
            # Colors should be generated from the supplied color scale
            # This section assumes rgb colors in the range 0-255.
            meta['mode'] = 'scaled'
            scale = color_scale
            if auto_scale:
                scale.set_range(min(rgb.ravel()), max(rgb.ravel()))
            rgb3 = np.zeros((s[0], s[1], 3), int)
            rgb3 = np.apply_along_axis(scale, 2, rgb)
            rgb = rgb3.astype(float) / 255.
            return (_fill_mask(rgb, mask, bg), meta)
        else:
            # Only one band of data to display but still need to determine how
            # to scale the data values
            meta['mode'] = 'monochrome'
            monochrome = True
            rgb = np.repeat(rgb, 3, 2).astype(float)

    # Perform any requested color enhancements.

    stretch = kwargs.get('stretch', settings.imshow_stretch)
    stretch_all = kwargs.get('stretch_all', settings.imshow_stretch_all)
    bounds = kwargs.get('bounds', None)

    if  bounds is not None:
        # Data limits for the color stretch are set explicitly
        bounds = np.array(bounds)
        if bounds.shape not in ((2,), (3, 2)):
            msg = '`bounds` keyword must have shape (2,) or (3, 2).'
            raise ValueError(msg)
        if bounds.ndim == 1:
            bounds = np.vstack((bounds,) * 3)
        rgb_lims = bounds
    else:
        # Determine data limits for color stretch from given cumulative
        # histogram values.
        if stretch in (True, False):
            msg = 'Boolean values for `stretch` keyword are deprected. See ' \
              'docstring for `get_rgb`'
            warnings.warn(msg)
            stretch = settings.imshow_stretch
        elif isinstance(stretch, Number):
            if not (0 <= stretch <= 1):
                raise ValueError('Value must be between 0 and 1.')
            stretch = (stretch, 1 - stretch)
        stretch = np.array(stretch)
        if stretch.shape not in ((2,), (3, 2)):
            raise ValueError("`stretch` keyword must be numeric or a " \
                             "sequence with shape (2,) or (3, 2).")
        nondata = kwargs.get('ignore', None)
        if stretch.ndim == 1:
            if monochrome:
                s = get_histogram_cdf_points(rgb[:, :, 0], stretch,
                                             ignore=nondata)
                rgb_lims = [s, s, s]
            elif stretch_all:
                # Stretch each color component independently
                rgb_lims = [get_histogram_cdf_points(rgb[:, :, i], stretch,
                                                     ignore=nondata) \
                            for i in range(3)]
            else:
                # Use a common lower/upper limit for each band by taking
                # the lowest lower limit and greatest upper limit.
                lims = np.array([get_histogram_cdf_points(rgb[:,:,i], stretch,
                                                          ignore=nondata) \
                        for i in range(3)])
                minmax = np.array([lims[:,0].min(), lims[:,1].max()])
                rgb_lims = minmax[np.newaxis, :].repeat(3, axis=0)
        else:
            if monochrome:
                # Not sure why anyone would want separate RGB stretches for
                # a gray-scale image but we'll let them.
                rgb_lims = [get_histogram_cdf_points(rgb[:,:,0], stretch[i],
                                                     ignore=nondata) \
                            for i in range(3)]
            elif stretch_all:
                rgb_lims = [get_histogram_cdf_points(rgb[:,:,i], stretch[i],
                                                     ignore=nondata) \
                            for i in range(3)]
            else:
                msg = 'Can not use common stretch if different stretch ' \
                  ' parameters are given for each color channel.'
                raise ValueError(msg)

    if 'mode' not in meta:
        meta['mode'] = 'rgb'
    meta['rgb range'] = rgb_lims
    for i in range(rgb.shape[2]):
        (lower, upper) = rgb_lims[i]
        span = upper - lower
        if lower == upper:
            rgb[:, :, i] = 0
        else:
            rgb[:, :, i] = np.clip((rgb[:, :, i] - lower) / span, 0, 1)
    return (_fill_mask(rgb, mask, bg), meta)

# For checking if valid keywords were supplied
_get_rgb_kwargs = ('stretch', 'stretch_all', 'bounds', 'colors', 'color_scale',
                   'auto_scale', 'ignore', 'mask', 'bg')

def running_ipython():
    '''Returns True if ipython is running.'''
    try:
        __IPYTHON__
        return True
    except NameError:
        return False


def warn_no_ipython():
    '''Warns that user is calling a GUI function outside of ipython.'''
    msg = '''
#############################################################################
SPy graphics functions are inteded to be run from IPython with the
`pylab` mode set for wxWindows.  For example,

    # ipython --pylab=WX

GUI functions will likely not function properly if you aren't running IPython
or haven't started it configured for pylab and wx.
#############################################################################
'''

    if sys.platform == 'darwin':
        msg += '''
NOTE: If you are running on Mac OS X and receive an error message
stating the following:

    "PyNoAppError: The wx.App object must be created first!",

You can avoid this error by running the following commandes immediately after
starting your ipython session:

    In [1]: import wx

    In [2]: app = wx.App()
#############################################################################
'''
    warnings.warn(msg, UserWarning)


def check_wx_app():
    '''Generates a warning if there is not a running wx.App.
    If spectral.START_WX_APP is True and there is no current app, then on will
    be started.
    '''
    import spectral
    import wx
    if wx.GetApp() is None and spectral.settings.START_WX_APP == True:
        warnings.warn('\nThere is no current wx.App object - creating one now.',
                      UserWarning)
        spectral.app = wx.App()