/usr/share/pyshared/chaco/datamapper.py is in python-chaco 4.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
CAUTION: This is an old file from Chaco 1.x to support the spatial subdivision
structures. It will be refactored soon.
If you are looking for Chaco's mappers (subclasses of AbstractMapper),
look in abstract_mapper.py, linear_mapper.py, and log_mapper.py.
Defines AbstractDataMapper and BruteForceDataMapper classes, and related trait
and functions.
"""
from numpy import array, concatenate, take, argsort, argmin, \
argmax, transpose, newaxis, sort
from traits.api import HasStrictTraits, Bool, Enum, Tuple, \
Property, Any, Float
#-------------------------------------------------------------------
# Module-specific traits
#-------------------------------------------------------------------
# Expresses sorting order of
ArraySortTrait = Enum('ascending', 'descending')
#-------------------------------------------------------------------
# Module-specific utility functions
#-------------------------------------------------------------------
def right_shift(ary, newval):
"Returns a right-shifted version of *ary* with *newval* inserted on the left."
return concatenate([[newval], ary[:-1]])
def left_shift(ary, newval):
"Returns a left-shifted version of *ary* with *newval* inserted on the right."
return concatenate([ary[1:], [newval]])
def sort_points(points, index=0):
"""
sort_points(array_of_points, index=<0|1>) -> sorted_array
Takes a list of points as an Nx2 array and sorts them according
to their x-coordinate (index=0) or y-coordinate (index=1).
"""
if len(points.shape) != 2 or (2 not in points.shape):
raise RuntimeError, "sort_points(): Array of wrong shape."
return take( points, argsort(points[:,index]) )
def array_zip(*arys):
"""
Returns a Numeric array that is the concatenation of the input 1-D
*arys* along a new axis. This function is basically equivalent to
``array(zip(*arys))``, but is more resource-efficient.
"""
return transpose(array(arys))
class AbstractDataMapper(HasStrictTraits):
"""
A data mapper maps from coordinate space to data elements. In its most
basic form, it loops over all the available data points to find the ones
near a given coordinate or within an area. More advanced functionality
includes returning rect-aligned "affected regions" enclosing all the
returned points, etc.
"""
# How to sort the output list of intersected points that the
# get_points_near_*() function returns. The points are always sorted
# by their domain (first/X-value) coordinate.
sort_order = ArraySortTrait
# A read-only property that describes the origin and size of the data
# set in data space as a 4-tuple (min_x, min_y, width, height)
extents = Property()
#-------------------------------------------------------------------
# Private traits
#-------------------------------------------------------------------
_data = Any
# Internally we expect Nx2 arrays; if the user hands in something
# different, we stored a transposed version but always remember to
# transpose once again whenever we return data.
_is_transposed = Bool(False)
# the max and min points in data space expressed as a 4-tuple (x,y,w,h)
_extents = Tuple
# a "fudge factor" to make the extents slightly larger than the actual
# values in the data set
_extents_delta = Float(0.1)
def __init__(self, data=None, data_sorting='none', **kw):
"See set_data() for description."
self._data = array([])
HasStrictTraits.__init__(self, **kw)
if data is not None:
self.set_data(data, data_sorting)
return
def get_points_near(self, pointlist, radius=0.0):
"""
get_points_near([points], radius=0.0) -> Nx2 array of candidate points
Returns a list of points near the input points (Nx2 array).
For each point in the input set, *radius* is used to create a
conceptual circle; if any points in the DataMapper's values lie inside
this circle, they are returned.
The returned list is not guaranteed to be a minimum or exact set,
but it is guaranteed to contain all points that intersect the
*pointlist*. The caller still must do fine-grained testing to see
if the points in the returned point list are a match.
"""
raise NotImplementedError
def get_points_near_polyline(self, line):
"""
get_points_near_polyline([v1, ... vN]) -> [ [points], [points], ... ]
This method is like get_points_near(), except that it takes a polyline
as input. A polyline is a list of vertices, each connected to the next
by a straight line. The polyline has infinitely thin width.
The input array can have shape 2xN or Nx2.
"""
raise NotImplementedError
def get_points_in_rect(self, rect):
"""
get_points_in_rect( (x,y,w,h) ) -> [ [points], [points], ... ]
This method is like get_points_near(), except that it takes a rectangle
as input. The rectangle has infinitely thin width.
"""
raise NotImplementedError
def get_points_in_poly(self, poly):
"""
get_points_in_poly([v1, ... vN]) -> [ [points], [points], ... ]
This method is like get_points_near(), except that it takes a polygon
as input. The polygon has infinitely thin width and can be
self-intersecting and concave.
The input array can have shape 2xN or Nx2.
"""
raise NotImplementedError
def get_last_region(self):
"""
Returns a region of screen space that contains all of the
points/lines/rect/polys in the last get_points_in_*() call. The
region returned by this method is guaranteed to only contain the points
that were returned by the previous call.
The region is returned as a list of (possibly disjoint) rectangles,
where each rectangle is a 4-tuple (x,y,w,h).
"""
raise NotImplementedError
def set_data(self, new_data, new_data_sorting='none'):
"""
set_data(new_data, new_data_sorting='none')
Sets the data used by this DataMapper. The *new_data_sorting* parameter
indicates how the new data is sorted: 'none', 'ascending', or 'descending'.
The default is 'none', which causes the data mapper to perform
a full sort of the input data.
The input data can be shaped 2xN or Nx2.
"""
if len(new_data) == 0:
self.clear()
return
if new_data.shape[0] == 2:
self._is_transposed = True
self._data = transpose(new_data)
else:
self._is_transposed = False
self._data = new_data
if new_data_sorting == 'none':
if self.sort_order == 'ascending':
self._data = sort_points(self._data)
else:
self._data = sort_points(self._data)[::-1]
elif new_data_sorting != self.sort_order:
self._data = self._data[::-1]
self._calc_data_extents()
self._update_datamap()
# a re-sorting is unnecessary because any internal data structures
# will have been updated by the _data update process.
return
def clear(self):
"""
clear()
Resets internal state and any cached data to reflect an empty
data set/data space.
"""
self._data = None
self._extents = (0,0,0,0)
self._clear()
return
def get_data(self):
"Returns the actual data used by the DataMapper."
if self._is_transposed:
return transpose(self._data)
else:
return self._data
#-------------------------------------------------------------------
# Concrete private methods and event handlers
# Child classes shouldn't have to override these.
#-------------------------------------------------------------------
def _get_extents(self):
return self._extents
def _calc_data_extents(self):
"""
Computes ((minX, minY), (width, height)) of self._data; sets self._extent and
returns nothing.
"""
if len(self._data) == 0:
self._extents = ((0,0), (0,0))
else:
value = self._data
min_indices = argmin(value, axis=0)
max_indices = argmax(value, axis=0)
x = value[min_indices[0], 0] - self._extents_delta
y = value[min_indices[1], 1] - self._extents_delta
maxX = value[max_indices[0], 0] + self._extents_delta
maxY = value[max_indices[1], 1] + self._extents_delta
self._extents = ((x, y), (maxX-x, maxY-y))
return
#-------------------------------------------------------------------
# Abstract private methods and event handlers
#-------------------------------------------------------------------
def _update_datamap(self):
"""
This function gets called after self._data has changed. Child classes
should implement this function if they need to recompute any cached
data structures, etc.
"""
return
def _clear(self):
"Performs subclass-specific clearing/cleanup."
return
def _sort_order_changed(self, old, new):
return
class BruteForceDataMapper(AbstractDataMapper):
"""
The BruteForceDataMapper returns all the points, all the time.
This is basically the same behavior as not having a data mapper in
the pipeline at all.
"""
def get_points_near(self, pointlist, radius=0):
return self.get_data()
def get_points_near_polyline(self, line):
return self.get_data()
def get_points_in_rect(self, rect):
return self.get_data()
def get_points_in_poly(self, poly):
return self.get_data()
def get_last_region(self):
return self._extents
def _sort_order_changed(self, old, new):
if len(self._data) == 0:
return
else:
if self.sort_order == 'ascending':
self._data = sort_points(self._data)
else:
self._data = sort_points(self._data)[::-1]
return
#EOF
|