Multidimensional array indexing order issues
What is the right way to index
multi-dimensional arrays? Before you jump to conclusions about the one and
true way to index multi-dimensional arrays, it pays to understand why this is
a confusing issue. This section will try to explain in detail how NumPy
indexing works and why we adopt the convention we do for images, and when it
may be appropriate to adopt other conventions.
The first thing to understand is
that there are two conflicting conventions for indexing 2-dimensional arrays.
Matrix notation uses the first index to indicate which row is being selected and
the second index to indicate which column is selected. This is opposite the
geometrically oriented-convention for images where people generally think the
first index represents x position (i.e., column) and the second represents y
position (i.e., row). This alone is the source of much confusion;
matrix-oriented users and image-oriented users expect two different things with
regard to indexing.
The second issue to understand is how indices correspond
to the order in which the array is stored in memory. In Fortran, the first index
is the most rapidly varying index when moving through the elements of a
two-dimensional array as it is stored in memory. If you adopt the matrix
convention for indexing, then this means the matrix is stored one column at a
time (since the first index moves to the next row as it changes). Thus Fortran
is considered a Column-major language. C has just the opposite convention. In
C, the last index changes most rapidly as one moves through the array as
stored in memory. Thus C is a Row-major language. The matrix is stored by
rows. Note that in both cases it presumes that the matrix convention for
indexing is being used, i.e., for both Fortran and C, the first index is the
row. Note this convention implies that the indexing convention is invariant
and that the data order changes to keep that so.
But that’s not the only way
to look at it. Suppose one has large two-dimensional arrays (images or
matrices) stored in data files. Suppose the data are stored by rows rather than
by columns. If we are to preserve our index convention (whether matrix or
image) that means that depending on the language we use, we may be forced to
reorder the data if it is read into memory to preserve our indexing
convention. For example, if we read row-ordered data into memory without
reordering, it will match the matrix indexing convention for C, but not for
Fortran. Conversely, it will match the image indexing convention for Fortran,
but not for C. For C, if one is using data stored in row order, and one wants
to preserve the image index convention, the data must be reordered when
reading into memory.
In the end, what you do for Fortran or C depends on
which is more important, not reordering data or preserving the indexing
convention. For large images, reordering data is potentially expensive, and
often the indexing convention is inverted to avoid that.
The situation with
NumPy makes this issue yet more complicated. The internal machinery of NumPy
arrays is flexible enough to accept any ordering of indices. One can simply
reorder indices by manipulating the internal stride information for
arrays without reordering the data at all. NumPy will know how to map the new
index order to the data without moving the data.
So if this is true, why not choose
the index order that matches what you most expect? In particular, why not define
row-ordered images to use the image convention? (This is sometimes referred
to as the Fortran convention vs the C convention, thus the ‘C’ and ‘FORTRAN’
order options for array ordering in NumPy.) The drawback of doing this is
potential performance penalties. It’s common to access the data sequentially,
either implicitly in array operations or explicitly by looping over rows of an
image. When that is done, then the data will be accessed in non-optimal order.
As the first index is incremented, what is actually happening is that elements
spaced far apart in memory are being sequentially accessed, with usually poor
memory access speeds. For example, for a two-dimensional image im defined so
that im[0, 10] represents the value at x = 0, y = 10. To be
consistent with usual Python behavior then im[0] would represent a column
at x = 0. Yet that data would be spread over the whole array since the data
are stored in row order. Despite the flexibility of NumPy’s indexing, it can’t
really paper over the fact basic operations are rendered inefficient because of
data order or that getting contiguous subarrays is still awkward (e.g.,
im[:, 0] for the first row, vs ). Thus one can’t use an idiom such
as for row in ; for col in does work, but doesn’t yield contiguous
column data.
As it turns out, NumPy is
smart enough when dealing with ufuncs to determine
which index is the most rapidly varying one in memory and uses that for the
innermost loop. Thus for ufuncs, there is no large intrinsic advantage to
either approach in most cases. On the other hand, use of ndarray.flat
with a FORTRAN ordered array will lead to non-optimal memory access as adjacent
elements in the flattened array (iterator, actually) are not contiguous in
memory.
Indeed, the fact is that Python
indexing on lists and other sequences naturally leads to an outside-to-inside
ordering (the first index gets the largest grouping, the next largest,
and the last gets the smallest element). Since image data are normally stored
in rows, this corresponds to the position within rows being the last item
indexed.
If you do want to use Fortran ordering realize that
there are two approaches to consider: 1) accept that the first index is just not
the most rapidly changing in memory and have all your I/O routines reorder
your data when going from memory to disk or visa versa, or use NumPy’s
mechanism for mapping the first index to the most rapidly varying data. We
recommend the former if possible. The disadvantage of the latter is that many
of NumPy’s functions will yield arrays without Fortran ordering unless you are
careful to use the order keyword. Doing this would be highly inconvenient.
Otherwise, we recommend simply learning to reverse the usual order of indices
when accessing elements of an array. Granted, it goes against the grain, but
it is more in line with Python semantics and the natural order of the data.