/usr/lib/python2.7/dist-packages/vigra/tagged_array.py is in python-vigra 1.10.0+git20160211.167be93+dfsg-5ubuntu1.
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 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 | #######################################################################
#
# Copyright 2009-2011 by Ullrich Koethe
#
# This file is part of the VIGRA computer vision library.
# The VIGRA Website is
# http://hci.iwr.uni-heidelberg.de/vigra/
# Please direct questions, bug reports, and contributions to
# ullrich.koethe@iwr.uni-heidelberg.de or
# vigra@informatik.uni-hamburg.de
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
#######################################################################
import copy, sys
import numpy
if sys.version_info[0] > 2:
xrange = range
def preserve_doc(f):
f.__doc__ = eval('numpy.ndarray.%s.__doc__' % f.__name__)
return f
class TaggedArray(numpy.ndarray):
'''
TaggedArray extends numpy.ndarray with an attribute 'axistags'. Any
axistags object must support the standard sequence interface, and its
length must match the number of dimensions of the array. Each item in
the axistags sequence is supposed to provide a description of the
corresponding array axis. All array functions that change the number or
ordering of an array's axes (such as transpose() and __getitem__()) are
overloaded so that they apply the same transformation to the axistags
object.
Example:
>>> axistags = ['x', 'y']
>>> a = TaggedArray((2,3), axistags=axistags)
>>> a.axistags
['x', 'y']
>>> a[:,0].axistags
['x']
>>> a[1,...].axistags
['y']
>>> a.transpose().axistags
['y', 'x']
Except for the new 'axistags' keyword, the 'TaggedArray' constructor is identical to the constructor
of 'numpy.ndarray'.
'''
def __new__(subtype, shape, dtype=float, buffer=None, offset=0, strides=None, order=None, axistags=None):
res = numpy.ndarray.__new__(subtype, shape, dtype, buffer, offset, strides, order)
if axistags is None:
res.axistags = res.default_axistags()
else:
if len(axistags) != res.ndim:
raise RuntimeError('TaggedArray(): len(axistags) must match ndim')
res.axistags = copy.copy(axistags)
return res
def default_axistags(self):
'''Create an axistags object with non-informative entries.
'''
return [None]*self.ndim
def copy_axistags(self):
'''Create a copy of 'self.axistags'. If the array doesn't have axistags, default_axistags()
will be returned.
'''
return copy.copy(getattr(self, 'axistags', self.default_axistags()))
def transpose_axistags(self, axes=None):
'''Create a copy of 'self.axistags' according to the given axes permutation
(internally called in transpose()).
'''
axistags = self.default_axistags()
if hasattr(self, 'axistags'):
if axes is None:
axes = range(self.ndim-1, -1, -1)
for k in xrange(self.ndim):
axistags[k] = self.axistags[int(axes[k])]
return axistags
def transform_axistags(self, index):
'''Create a copy of 'self.axistags' according to the given index or slice object
(internally called in __getitem__()).
'''
# we assume that self.ndim is already set to its new value, whereas
# self.axistags has just been copied by __array_finalize__
new_axistags = self.default_axistags()
if hasattr(self, 'axistags'):
old_axistags = self.axistags
old_ndim = len(old_axistags)
new_ndim = len(new_axistags)
try:
# make sure that 'index' is a tuple
len_index = len(index)
except:
index = (index,)
len_index = 1
len_index -= index.count(numpy.newaxis)
if len_index < old_ndim and index.count(Ellipsis) == 0:
index += (Ellipsis,)
len_index += 1
# how many missing axes are represented by an Ellipsis ?
len_ellipsis = old_ndim - len_index
knew, kold, kindex = 0, 0, 0
while knew < new_ndim:
try:
# if index[kindex] is int, the dimension is bound => drop this axis
int(index[kindex])
kold += 1
kindex += 1
except:
if index[kindex] is not numpy.newaxis:
# copy the tag
new_axistags[knew] = old_axistags[kold]
kold += 1
knew += 1
# the first ellipsis represents all missing axes
if len_ellipsis > 0 and index[kindex] is Ellipsis:
len_ellipsis -= 1
else:
kindex += 1
return new_axistags
__array_priority__ = 10.0
def __array_finalize__(self, obj):
if hasattr(obj, 'axistags'):
self.axistags = obj.axistags
@preserve_doc
def __copy__(self, order = 'C'):
result = numpy.ndarray.__copy__(self, order)
result.axistags = result.copy_axistags()
return result
@preserve_doc
def __deepcopy__(self, memo):
result = numpy.ndarray.__deepcopy__(self, memo)
memo[id(self)] = result
result.__dict__ = copy.deepcopy(self.__dict__, memo)
return result
def __repr__(self):
return "%s(shape=%s, axistags=%s, dtype=%s, data=\n%s)" % \
(self.__class__.__name__, str(self.shape), repr(self.axistags), str(self.dtype), str(self))
@preserve_doc
def all(self, axis=None, out=None):
res = numpy.ndarray.all(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def any(self, axis=None, out=None):
res = numpy.ndarray.any(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def argmax(self, axis=None, out=None):
res = numpy.ndarray.argmax(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def argmin(self, axis=None, out=None):
res = numpy.ndarray.argmin(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def cumsum(self, axis=None, dtype=None, out=None):
res = numpy.ndarray.cumsum(self, axis, dtype, out)
if res.ndim != self.ndim:
res.axistags = res.default_axistags()
return res
@preserve_doc
def cumprod(self, axis=None, dtype=None, out=None):
res = numpy.ndarray.cumprod(self, axis, dtype, out)
if res.ndim != self.ndim:
res.axistags = res.default_axistags()
return res
# FIXME: we should also provide a possibility to determine flattening order by axistags
# (the same applies to flat and ravel)
@preserve_doc
def flatten(self, order='C'):
res = numpy.ndarray.flatten(self, order)
res.axistags = res.default_axistags()
return res
@preserve_doc
def max(self, axis=None, out=None):
res = numpy.ndarray.max(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def mean(self, axis=None, out=None):
res = numpy.ndarray.mean(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def min(self, axis=None, out=None):
res = numpy.ndarray.min(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def nonzero(self):
res = numpy.ndarray.nonzero(self)
for k in xrange(len(res)):
res[k].axistags = copy.copy(self.axistags[k])
return res
@preserve_doc
def prod(self, axis=None, dtype=None, out=None):
res = numpy.ndarray.prod(self, axis, dtype, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def ptp(self, axis=None, out=None):
res = numpy.ndarray.ptp(self, axis, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def ravel(self, order='C'):
res = numpy.ndarray.ravel(self, order)
res.axistags = res.default_axistags()
return res
@preserve_doc
def repeat(self, repeats, axis=None):
res = numpy.ndarray.repeat(self, repeats, axis)
if axis is None:
res.axistags = res.default_axistags()
return res
@preserve_doc
def reshape(self, shape, order='C'):
res = numpy.ndarray.reshape(self, shape, order)
res.axistags = res.default_axistags()
return res
@preserve_doc
def resize(self, new_shape, refcheck=True, order=False):
res = numpy.ndarray.reshape(self, new_shape, refcheck, order)
res.axistags = res.default_axistags()
return res
@preserve_doc
def squeeze(self):
res = numpy.ndarray.squeeze(self)
if self.ndim != res.ndim:
res.axistags = res.copy_axistags()
for k in xrange(self.ndim-1, -1, -1):
if self.shape[k] == 1:
del res.axistags[k]
return res
@preserve_doc
def std(self, axis=None, dtype=None, out=None, ddof=0):
res = numpy.ndarray.std(self, axis, dtype, out, ddof)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
if len(res.shape) == 0:
res = res.item()
return res
@preserve_doc
def sum(self, axis=None, dtype=None, out=None):
res = numpy.ndarray.sum(self, axis, dtype, out)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
return res
@preserve_doc
def swapaxes(self, i, j):
res = numpy.ndarray.swapaxes(self, i, j)
res.axistags = res.copy_axistags()
res.axistags[i], res.axistags[j] = res.axistags[j], res.axistags[i]
return res
@preserve_doc
def take(self, indices, axis=None, out=None, mode='raise'):
res = numpy.ndarray.take(self, indices, axis, out, mode)
if axis is None:
res.axistags = res.default_axistags()
return res
@preserve_doc
def transpose(self, *axes):
res = numpy.ndarray.transpose(self, *axes)
res.axistags = res.transpose_axistags(*axes)
return res
@preserve_doc
def var(self, axis=None, dtype=None, out=None, ddof=0):
res = numpy.ndarray.var(self, axis, dtype, out, ddof)
if axis is not None:
res.axistags = res.copy_axistags()
del res.axistags[axis]
if len(res.shape) == 0:
res = res.item()
return res
@property
def T(self):
return self.transpose()
def __getitem__(self, index):
'''x.__getitem__(y) <==> x[y]
In addition to the usual indexing functionality, this function
also updates the axistags of the result array. There are three cases:
* getitem creates a scalar value => no axistags are required
* getitem creates an arrayview => axistags are transferred from the
corresponding axes of the base array,
axes resulting from 'newaxis' get tag 'None'
* getitem creates a copy of an array (fancy indexing) => all axistags are 'None'
'''
res = numpy.ndarray.__getitem__(self, index)
if res is not self and hasattr(res, 'axistags'):
if res.base is self:
res.axistags = res.transform_axistags(index)
else:
res.axistags = res.default_axistags()
return res
|