The following produces a C-contiguous numpy array:
import numpy
a = numpy.ones((1024,1024,5))
Now if I slice it, the result may not longer
As things stand, any attempt to coerce the slice bn
to C contiguous order is going to create a copy.
If you don't want to change the shapes you're starting with (and don't need a
itself in C order), one possible solution is to start with the array a
in Fortran order:
>>> a = numpy.ones((1024, 1024, 5), order='f')
The slices are also then F-contiguous:
>>> bn = a[:, :, 0]
>>> bn.flags
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
...
This means that the transpose of the slice bn
will be in C order and transposing does not create a copy:
>>> bn.T.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
...
And you can then hash the slice:
>>> hashlib.sha1(bn.T).hexdigest()
'01dfa447dafe16b9a2972ce05c79410e6a96840e'