~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to chaco/_speedups_fallback.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
_speedups extension module.
4
4
"""
5
5
 
6
 
from numpy import invert, isnan, array, transpose, zeros, compress
 
6
from numpy import clip, invert, isnan, isinf, array, transpose, zeros, \
 
7
    compress, where, take, float32, ones_like
 
8
import numpy as np
 
9
 
7
10
import operator
8
11
 
9
12
def array_combine(a, b, op=operator.and_, func=lambda x: x):
108
111
        selections = None
109
112
    return points, selections
110
113
 
 
114
 
 
115
 
 
116
def apply_selection_fade(mapped_image, mask, fade_alpha, fade_background):
 
117
    '''Apply a selection fade to a colormapped image.
 
118
 
 
119
    Parameters
 
120
    ----------
 
121
    mapped_image : ndarray of uint8, shape (N,M,4)
 
122
        The digitized rgba values
 
123
    mask : ndarray of bool, shape (N,M,4)
 
124
        The array of masked pixels
 
125
    fade_alpha : float
 
126
        The alpha value for the fade
 
127
    fade_background : rgb888 tuple
 
128
        The fade background
 
129
 
 
130
    '''
 
131
    imask = invert(mask)
 
132
    if fade_alpha == 0:
 
133
        mapped_image[imask,0:3] = fade_background
 
134
    else:
 
135
        ialpha = (1.0 - fade_alpha)
 
136
        background = tuple(ialpha * x for x in fade_background)
 
137
        image_region = mapped_image[imask,0:3]
 
138
        image_region *= fade_alpha
 
139
        image_region += background
 
140
        mapped_image[imask,0:3] = image_region
 
141
 
 
142
 
 
143
def map_colors(data_array, steps, low, high, red_lut, green_lut, blue_lut,
 
144
        alpha_lut):
 
145
    '''Map colors from color lookup tables to a data array.
 
146
 
 
147
    This is used in ColorMapper.map_screen
 
148
 
 
149
    Parameters
 
150
    ----------
 
151
    data_array : ndarray
 
152
        The data array
 
153
    steps: int
 
154
        The number of steps in the color map (depth)
 
155
    low : float
 
156
        The low end of the data range
 
157
    high : float
 
158
        The high end of the data range
 
159
    red_lut : ndarray of float32
 
160
        The red channel lookup table
 
161
    green_lut : ndarray of float32
 
162
        The green channel lookup table
 
163
    blue_lut : ndarray of float32
 
164
        The blue channel lookup table
 
165
    alpha_lut : ndarray of float32
 
166
        The alpha channel lookup table
 
167
    
 
168
    Returns
 
169
    -------
 
170
    rgba: ndarray of float32
 
171
        The rgba values of data_array according to the lookup tables. The shape
 
172
        of this array is equal to data_array.shape + (4,).
 
173
 
 
174
    '''
 
175
    range_diff = high - low
 
176
 
 
177
    if range_diff == 0.0 or isinf(range_diff):
 
178
        # Handle null range, or infinite range (which can happen during 
 
179
        # initialization before range is connected to a data source).
 
180
        norm_data = 0.5*ones_like(data_array)
 
181
    else:
 
182
        norm_data = clip((data_array - low) / range_diff, 0.0, 1.0)
 
183
 
 
184
 
 
185
    nanmask = isnan(norm_data)
 
186
    norm_data = where(nanmask, 0, (norm_data * (steps-1)).astype(int))
 
187
    rgba = zeros(norm_data.shape+(4,), float32)
 
188
    rgba[...,0] = where(nanmask, 0, take(red_lut, norm_data))
 
189
    rgba[...,1] = where(nanmask, 0, take(green_lut, norm_data))
 
190
    rgba[...,2] = where(nanmask, 0, take(blue_lut, norm_data))
 
191
    rgba[...,3] = where(nanmask, 0, take(alpha_lut, norm_data))
 
192
 
 
193
    return rgba
 
194
 
 
195
def map_colors_uint8(data_array, steps, low, high, red_lut, green_lut, blue_lut,
 
196
        alpha_lut):
 
197
    '''Map colors from color lookup tables to a data array.
 
198
 
 
199
    This is used in ColorMapper.map_screen
 
200
 
 
201
    Parameters
 
202
    ----------
 
203
    data_array : ndarray
 
204
        The data array
 
205
    steps: int
 
206
        The number of steps in the color map (depth)
 
207
    low : float
 
208
        The low end of the data range
 
209
    high : float
 
210
        The high end of the data range
 
211
    red_lut : ndarray of uint8
 
212
        The red channel lookup table
 
213
    green_lut : ndarray of uint8
 
214
        The green channel lookup table
 
215
    blue_lut : ndarray of uint8
 
216
        The blue channel lookup table
 
217
    alpha_lut : ndarray of uint8
 
218
        The alpha channel lookup table
 
219
    
 
220
    Returns
 
221
    -------
 
222
    rgba: ndarray of uint8
 
223
        The rgba values of data_array according to the lookup tables. The shape
 
224
        of this array is equal to data_array.shape + (4,).
 
225
 
 
226
    '''
 
227
    range_diff = high - low
 
228
 
 
229
    if range_diff == 0.0 or isinf(range_diff):
 
230
        # Handle null range, or infinite range (which can happen during 
 
231
        # initialization before range is connected to a data source).
 
232
        norm_data = 0.5*ones_like(data_array)
 
233
    else:
 
234
        norm_data = clip((data_array - low) / range_diff, 0.0, 1.0)
 
235
 
 
236
 
 
237
    nanmask = isnan(norm_data)
 
238
    norm_data = where(nanmask, 0, (norm_data * (steps-1)).astype('uint8'))
 
239
    rgba = zeros(norm_data.shape+(4,), dtype='uint8')
 
240
    rgba[...,0] = where(nanmask, 0, take(red_lut, norm_data))
 
241
    rgba[...,1] = where(nanmask, 0, take(green_lut, norm_data))
 
242
    rgba[...,2] = where(nanmask, 0, take(blue_lut, norm_data))
 
243
    rgba[...,3] = where(nanmask, 0, take(alpha_lut, norm_data))
 
244
 
 
245
    return rgba
 
246