python - Cheapest way to get a numpy array into C-contiguous order? -


the following produces c-contiguous numpy array:

import numpy  = numpy.ones((1024,1024,5)) 

now if slice it, result may not longer same. example:

bn = a[:, :, n] 

with n 0 4. problem need bn c-contiguous, , need many instances of a. need each bn once, , want avoid doing

bn  = bn.copy(order='c') 

i don't want rewrite code such that

a = numpy.ones((5,1024,1024)) 

is there faster, cheaper way bn doing copy?

background:

i want hash each slice of every a, using

import hashlib  hashlib.sha1(a[:, :, n]).hexdigest() 

unfortunately, throw valueerror, complaining order. if there fast way hash want, i'd use it.

as things stand, attempt coerce slice bn c contiguous order going create copy.

if don't want change shapes you're starting (and don't need a in c order), 1 possible solution start array a in fortran order:

>>> = numpy.ones((1024, 1024, 5), order='f') 

the slices f-contiguous:

>>> bn = a[:, :, 0] >>> bn.flags   c_contiguous : false   f_contiguous : true   owndata : false   ... 

this means transpose of slice bn in c order , transposing not create copy:

>>> bn.t.flags   c_contiguous : true   f_contiguous : false   owndata : false   ... 

and can hash slice:

>>> hashlib.sha1(bn.t).hexdigest() '01dfa447dafe16b9a2972ce05c79410e6a96840e' 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -