Skip to content

Instantly share code, notes, and snippets.

@okapies
Last active Mar 20, 2019
Embed
What would you like to do?
How `numpy.array(order='F')` works
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b')
array([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]], dtype=int8)
>>> ctypes.string_at(np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b').ctypes.data, 8) # .data.tobytes() doesn't work properly
b'\x00\x04\x02\x06\x01\x05\x03\x07'
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b').__array_interface__
{'data': (23426096, False), 'strides': None, 'descr': [('', '|i1')], 'typestr': '|i1', 'shape': (2, 2, 2), 'version': 3}
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b').flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F')
array([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]], dtype=int8)
>>> ctypes.string_at(np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').ctypes.data, 8)
b'\x00\x01\x02\x03\x04\x05\x06\x07'
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').__array_interface__
{'data': (23304720, False), 'strides': (1, 2, 4), 'descr': [('', '|i1')], 'typestr': '|i1', 'shape': (2, 2, 2), 'version': 3}
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').flags
C_CONTIGUOUS : False
F_CONTIGUOUS : True # `order` does not configure it directly; it is just computed from the current shape and strides
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').T
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]], dtype=int8)
>>> ctypes.string_at(np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').T.ctypes.data, 8)
b'\x00\x01\x02\x03\x04\x05\x06\x07'
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').T.__array_interface__
{'data': (23426096, False), 'strides': None, 'descr': [('', '|i1')], 'typestr': '|i1', 'shape': (2, 2, 2), 'version': 3}
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b', order='F').T.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
import ctypes
import numpy as np
from numpy.lib import stride_tricks
def from_array_to_bytes(a):
return ctypes.string_at(a.ctypes.data, a.nbytes)
def print_array(desc, a):
print(desc + " ->")
print(" " + str(a.tolist()))
print(" underlying: {}, strides: {}, owndata: {: >5}, c: {: >5}, f:{: >5}, data: {: >15}".format(
from_array_to_bytes(a),
a.strides,
str(a.flags.owndata),
str(a.flags.c_contiguous),
str(a.flags.f_contiguous),
a.ctypes.data,
))
def main():
a0 = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype="b")
print_array("array(list)", a0)
#print_array("array(list).view()", a0.view())
#print_array("array(list).reshape((2, 2, 2)))", a0.reshape((2, 2, 2)))
#print_array("array(list).reshape((2, 2, 2), order='f'))", a0.reshape((2, 2, 2), order='f'))
print()
a1 = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype="b", order="f")
print_array("array(list, order='f')", a1)
print()
#b0 = np.frombuffer(b"\x00\x01\x02\x03\x04\x05\x06\x07", dtype="b")
#print_array("frombuffer(bytes).reshape((2, 2, 2))", b0.reshape(2, 2, 2))
#print_array("frombuffer(bytes).reshape((2, 2, 2)).view()", b0.reshape(2, 2, 2).view())
#print_array("frombuffer(bytes).reshape((2, 2, 2), order='f')", b0.reshape(2, 2, 2, order='f'))
#print_array("frombuffer(bytes).reshape((2, 2, 2)).reshape((2, 2, 2), order='f')", b0.reshape(2, 2, 2).reshape(2, 2, 2, order='f'))
#print()
c0 = np.array([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b')], dtype="b")
print_array("array(list[ndarray(c), ndarray(c)])", c0)
c1 = np.array([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b')], dtype="b", order='f')
print_array("array(list[ndarray(c), ndarray(c)], order='f')", c1)
c2 = np.array([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b')], dtype="b")
print_array("array(list[ndarray(f), ndarray(c)])", c2)
c3 = np.array([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b')], dtype="b", order='f')
print_array("array(list[ndarray(f), ndarray(c)], order='f')", c3)
#c4 = np.array([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b', order='f')], dtype="b", order='a')
#print_array("array(list[ndarray(c), ndarray(f)], order='a')", c4)
#c5 = np.array([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b', order='f')], dtype="b", order='k')
#print_array("array(list[ndarray(c), ndarray(f)], order='k')", c5)
c6 = np.array([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')], dtype="b")
print_array("array(list[ndarray(f), ndarray(f)])", c6)
c7 = np.array([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')], dtype="b", order='f')
print_array("array(list[ndarray(f), ndarray(f)], order='f')", c7)
print()
c8 = np.stack([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b')])
print_array("stack(list[ndarray(c), ndarray(c)])", c8)
c9 = np.asfortranarray(np.stack([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b')]))
print_array("asfortranarray(stack(list[ndarray(c), ndarray(c)]))", c9)
print()
c11 = np.stack([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b', order='f')])
print_array("stack(list[ndarray(c), ndarray(f)])", c11)
c12 = np.asfortranarray(np.stack([np.array([[0, 1], [2, 3]], dtype='b'), np.array([[4, 5], [6, 7]], dtype='b', order='f')]))
print_array("asfortranarray(stack(list[ndarray(c), ndarray(f)]))", c12)
print()
c12 = np.stack([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')])
print_array("stack(list[ndarray(f), ndarray(f)])", c12)
c13 = np.asfortranarray(np.stack([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')]))
print_array("asfortranarray(stack(list[ndarray(f), ndarray(f)]))", c13)
print()
c14 = np.stack([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')], axis=-1)
print_array("stack(list[ndarray(f), ndarray(f)], axis=-1)", c14)
c15 = np.stack([np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')], axis=1)
print_array("stack(list[ndarray(f), ndarray(f)], axis=1)", c15)
print()
d6 = [np.array([[0, 1], [2, 3]], dtype='b', order='c'), np.array([[4, 5], [6, 7]], dtype='b', order='c')]
d7 = np.frombuffer(bytearray([0x00] * 8), dtype='b').reshape((2, 2, 2), order='C')
print_array("concatenate(list[ndarray(c)[None], ndarray(c)[None]], axis=0)", d7)
np.concatenate([e[None] for e in d6], axis=0, out=d7)
print_array("concatenate(list[ndarray(c)[None], ndarray(c)[None]], axis=0)", d7)
print()
d8 = [np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='c')]
d9 = np.frombuffer(bytearray([0x00] * 8), dtype='b').reshape((2, 2, 2), order='C')
print_array("concatenate(list[ndarray(f)[None], ndarray(c)[None]], axis=0)", d9)
np.concatenate([e[None] for e in d6], axis=0, out=d9)
print_array("concatenate(list[ndarray(f)[None], ndarray(c)[None]], axis=0)", d9)
print()
d0 = [np.array([[0, 1], [2, 3]], dtype='b', order='f'), np.array([[4, 5], [6, 7]], dtype='b', order='f')]
d1 = [e[None] for e in d0]
d2 = np.frombuffer(bytearray([0x00] * 8), dtype='b').reshape((2, 2, 2), order='C')
print_array("concatenate(list[ndarray(f)[None], ndarray(f)[None]], axis=0)", d2)
np.concatenate(d1, axis=0, out=d2)
print_array("concatenate(list[ndarray(f)[None], ndarray(f)[None]], axis=0)", d2)
#d10 = np.ascontiguousarray(d2)
#print_array("ascontiguoussrray(concatenate(list[ndarray(f)[None], ndarray(f)[None]], axis=0))", d10)
#d11 = np.asfortranarray(d2)
#print_array("asfortranarray(concatenate(list[ndarray(f)[None], ndarray(f)[None]], axis=0))", d11)
print()
#d3 = [e[:,None] for e in d0]
#d4 = np.concatenate(d3, axis=1)
#print_array("concatenate(list[ndarray(f)[:,None], ndarray(f)[:,None]], axis=1)", d4)
#d5 = stride_tricks.as_strided(d4, (2, 2, 2), (1, 2, 4))
#print_array("as_strided(concatenate(list[ndarray(f)[:,None], ndarray(f)[:,None]], axis=1), shape=(2, 2, 2), strides=(1, 2, 4))", d5)
#print()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment