~/bend-docscommunity

npy.bend checks

raw source on the hub · import bend-ndarray@0.1.4.0/npy.bend as Npy

bend-ndarray's .npy files, pure: an array to the bytes of a .npy file (version 1.0, little-endian, C order), and the bytes of a .npy file (versions 1, 2 and 3) back to an array. Reading and writing the files: io.bend. float64 loads as float64, bit for bit (NaN payloads too). Other dtypes are converted as they load: float16 to float32 (exactly); int8, int16 and uint16 to int32 (exactly); int64 to int32 and uint64 to uint32 when every value fits, else the load is refused naming the first that does not. Fortran-order arrays are transposed into row-major order and big-endian ones byte-swapped, so an array loads as np.load reads it.

5 imports
import Base
import ./shape.bend as S
import ./cells.bend as C
import ./tree.bend as T
import ./ndarray.bend as NP

Types

type Tok source · line 175 · raw

Data

type Lex source · line 181 · raw

Data

type LexT source · line 200 · raw

Data

the state after one byte from idle, and the token list

type Hdr source · line 304 · raw

Data

type Elt source · line 321 · raw

Data

how a stored element becomes a cell: its kind (f, i, u, b), its size in bytes, and the dtype it loads as

type Mode source · line 372 · raw

Data

how one stored element becomes a cell, decided once per file

Definitions

def b0 source · line 15 · raw

@+x:U32 -> U32

def b1 source · line 18 · raw

@+x:U32 -> U32

def b2 source · line 21 · raw

@+x:U32 -> U32

def b3 source · line 24 · raw

@+x:U32 -> U32

def le4 source · line 27 · raw

@xs:List<&2, U32> -> List<&2, U32>

def le1 source · line 34 · raw

@xs:List<&2, U32> -> List<&2, U32>

def ascii source · line 41 · raw

@s:String -> List<&2, U32>

def text source · line 48 · raw

@bs:List<&2, U32> -> String

def btake source · line 55 · raw

@n:Nat -> @xs:List<&2, U32> -> List<&2, U32>

def bdrop source · line 62 · raw

@n:Nat -> @xs:List<&2, U32> -> List<&2, U32>

def bnth source · line 69 · raw

@xs:List<&2, U32> -> @n:Nat -> U32

def spaces source · line 78 · raw

@n:Nat -> List<&2, U32>

def top_bit source · line 88 · raw

@fuel:Nat -> @+m:U32 -> @+p:Nat -> Nat

the highest set bit of m (1 .. 1023), counted from 0

def f16_sub source · line 95 · raw

@+sign:U32 -> @+m:U32 -> @+p:Nat -> U32

def f16_parts source · line 98 · raw

@+sign:U32 -> @+e:U32 -> @+m:U32 -> U32

def f16_to_f32 source · line 106 · raw

@+h:U32 -> U32

def descr source · line 109 · raw

@dt:0xb394b5997edb05025e33977930018c47/shape.DType -> String

def wide source · line 124 · raw

@dt:0xb394b5997edb05025e33977930018c47/shape.DType -> Bool

def header_text source · line 134 · raw

@+dt:0xb394b5997edb05025e33977930018c47/shape.DType -> @+s:List<&2, Nat> -> List<&2, U32>

the header dict as np.save writes it

def header_padded source · line 138 · raw

@+h:List<&2, U32> -> List<&2, U32>

padded with spaces and a newline so the data starts at a multiple of 64 bytes

def data_bytes source · line 141 · raw

@w:Bool -> @cells:List<&2, U32> -> List<&2, U32>

def lo_first source · line 149 · raw

@xs:List<&2, U32> -> List<&2, U32>

a float64 element's cells (high word, low word) in the order its bytes are stored (low word first)

def stored source · line 156 · raw

@dt:0xb394b5997edb05025e33977930018c47/shape.DType -> @xs:List<&2, U32> -> List<&2, U32>

def file_bytes source · line 163 · raw

@+dt:0xb394b5997edb05025e33977930018c47/shape.DType -> @+h:List<&2, U32> -> @cells:List<&2, U32> -> List<&2, U32>

def to_npy source · line 168 · raw

@a:0xb394b5997edb05025e33977930018c47/ndarray.ND -> List<&2, U32>

the bytes of a .npy file holding a (version 1.0: the array's dtype, little-endian, C order)

def is_digit source · line 187 · raw

@+b:U32 -> Bool

def is_alpha source · line 190 · raw

@+b:U32 -> Bool

def is_quote source · line 193 · raw

@+b:U32 -> Bool

def is_mark source · line 196 · raw

@+b:U32 -> Bool

def idle_byte source · line 203 · raw

@+b:U32 -> @+out:List<&2, Tok> -> LexT

def lex_step source · line 209 · raw

@l:LexT -> @+b:U32 -> LexT

def lex_go source · line 220 · raw

@bs:List<&2, U32> -> @l:LexT -> LexT

def lex_end source · line 227 · raw

@l:LexT -> List<&2, Tok>

def lex source · line 237 · raw

@bs:List<&2, U32> -> List<&2, Tok>

the header's tokens: strings, words, numbers and the marks { } ( ) : ,

def bytes_eq source · line 240 · raw

@a:List<&2, U32> -> @b:List<&2, U32> -> Bool

def is_key source · line 249 · raw

@t:Tok -> @+k:List<&2, U32> -> Bool

def bdrop_tok source · line 257 · raw

@ts:List<&2, Tok> -> List<&2, Tok>

the tokens after the key k and its colon

def after_key source · line 264 · raw

@ts:List<&2, Tok> -> @+k:List<&2, U32> -> List<&2, Tok>

def str_val source · line 271 · raw

@ts:List<&2, Tok> -> Maybe<&2, List<&2, U32>>

def bool_val source · line 278 · raw

@ts:List<&2, Tok> -> Maybe<&2, Bool>

def nums_go source · line 286 · raw

@ts:List<&2, Tok> -> @+acc:List<&2, Nat> -> Maybe<&2, List<&2, Nat>>

the numbers of a tuple: from its ( to its ), skipping commas and Python 2's L suffixes

def tuple_val source · line 297 · raw

@ts:List<&2, Tok> -> Maybe<&2, List<&2, Nat>>

def hdr_of source · line 307 · raw

@d:Maybe<&2, List<&2, U32>> -> @f:Maybe<&2, Bool> -> @s:Maybe<&2, List<&2, Nat>> -> Result<&2, &2, 0xb394b5997edb05025e33977930018c47/shape.NdErr, Hdr>

def kind_of source · line 324 · raw

@+k:U32 -> @+n:Nat -> @+big:Bool -> Maybe<&2, Elt>

def num_go source · line 336 · raw

@bs:List<&2, U32> -> @+acc:Nat -> Nat

descr: a byte order (<, >, =, | or none), a kind letter, a size in bytes

def num_of source · line 343 · raw

@bs:List<&2, U32> -> Nat

def parse_descr source · line 346 · raw

@d:List<&2, U32> -> Maybe<&2, Elt>

def ordered source · line 355 · raw

@+big:Bool -> @+bs:List<&2, U32> -> List<&2, U32>

the element's bytes, lowest first

def w4 source · line 358 · raw

@+bs:List<&2, U32> -> U32

def w2 source · line 361 · raw

@+bs:List<&2, U32> -> U32

def sext source · line 364 · raw

@+x:U32 -> @+bits:U32 -> @+n:Nat -> U32

def wide_int source · line 368 · raw

@+signed:Bool -> @+lo:U32 -> @+hi:U32 -> Maybe<&2, U32>

a 64-bit integer as a 32-bit cell: signed (int64 to int32) or not (uint64 to uint32), if it fits

def mode_of source · line 385 · raw

@k:Elt -> Mode

def one source · line 393 · raw

@m:Maybe<&2, U32> -> Maybe<&2, List<&2, U32>>

def elem source · line 402 · raw

@m:Mode -> @+bs:List<&2, U32> -> Maybe<&2, List<&2, U32>>

one element (its bytes lowest first) as its cells (a float64's two, the high word first), or None when it does not fit

def push source · line 428 · raw

@xs:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>

the cells pushed onto acc (which is reversed at the end)

def elem_got source · line 435 · raw

@r:Maybe<&2, List<&2, U32>> -> @+i:Nat -> @acc:List<&2, U32> -> @+name:String -> @more:(@_:Unit -> @_:List<&2, U32> -> Result<&2, &2, 0xb394b5997edb05025e33977930018c47/shape.NdErr, List<&2, U32>>) -> Result<&2, &2, 0xb394b5997edb05025e33977930018c47/shape.NdErr, List<&2, U32>>

def elems_go source · line 443 · raw

@n:Nat -> @+k:Mode -> @+size:Nat -> @+big:Bool -> @+i:Nat -> @+bs:List<&2, U32> -> @acc:List<&2, U32> -> @+name:String -> Result<&2, &2, 0xb394b5997edb05025e33977930018c47/shape.NdErr, List<&2, U32>>

def kind_size source · line 450 · raw

@k:Elt -> Nat

def kind_big source · line 455 · raw

@k:Elt -> Bool

def kind_dt source · line 460 · raw

@k:Elt -> 0xb394b5997edb05025e33977930018c47/shape.DType

def fortran_fix source · line 467 · raw

@f:Bool -> @a:0xb394b5997edb05025e33977930018c47/ndarray.ND -> 0xb394b5997edb05025e33977930018c47/ndarray.ND

def shaped source · line 474 · raw

@r:Result<&2, &2, 0xb394b5997edb05025e33977930018c47/shape.NdErr, List<&2, U32>> -> @+dt:0xb394b5997edb05025e33977930018c47/shape.DType -> @+s:List<&2, Nat> -> @+fortran:Bool -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def enough source · line 482 · raw

@+need:Nat -> @+have:Nat -> Bool

a Fortran-order array is stored as its transpose in C order

def kind_go source · line 485 · raw

@ok:Bool -> @+k:Elt -> @+d:List<&2, U32> -> @+f:Bool -> @+s:List<&2, Nat> -> @+data:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def with_kind source · line 492 · raw

@m:Maybe<&2, Elt> -> @h:Hdr -> @+data:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def hdr_descr source · line 499 · raw

@h:Hdr -> List<&2, U32>

def with_header source · line 504 · raw

@r:Result<&2, &2, 0xb394b5997edb05025e33977930018c47/shape.NdErr, Hdr> -> @+data:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def body source · line 511 · raw

@+start:Nat -> @+hlen:Nat -> @+bs:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def v_two source · line 514 · raw

@ok:Bool -> @+v:U32 -> @+bs:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def v_one source · line 521 · raw

@is1:Bool -> @+v:U32 -> @+bs:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def magic_ok source · line 528 · raw

@m:Bool -> @+bs:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

def from_npy source · line 536 · raw

@+bs:List<&2, U32> -> Result<&1, &1, 0xb394b5997edb05025e33977930018c47/shape.NdErr, 0xb394b5997edb05025e33977930018c47/ndarray.ND>

the array in a .npy file's bytes