/usr/share/pyshared/nipy/utils/arrays.py is in python-nipy 0.3.0-1ubuntu2.
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 | """ Array utilities
"""
import numpy as np
def strides_from(shape, dtype, order='C'):
""" Return strides as for continuous array shape `shape` and given `dtype`
Parameters
----------
shape : sequence
shape of array to calculate strides from
dtype : dtype-like
dtype specifier for array
order : {'C', 'F'}, optional
whether array is C or FORTRAN ordered
Returns
-------
strides : tuple
seqence length ``len(shape)`` giving strides for continuous array with
given `shape`, `dtype` and `order`
Examples
--------
>>> strides_from((2,3,4), 'i4')
(48, 16, 4)
>>> strides_from((3,2), np.float)
(16, 8)
>>> strides_from((5,4,3), np.bool, order='F')
(1, 5, 20)
"""
dt = np.dtype(dtype)
if dt.itemsize == 0:
raise ValueError('Empty dtype "%s"' % dt)
if order == 'F':
strides = np.cumprod([dt.itemsize] + list(shape[:-1]))
elif order == 'C':
strides = np.cumprod([dt.itemsize] + list(shape)[::-1][:-1])
strides = strides[::-1]
else:
raise ValueError('Unexpected order "%s"' % order)
return tuple(strides)
|