npy.bend checks
raw source on the hub · import bend-ndarray@0.1.3.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. Other dtypes are converted as they load: float64 to float32 (rounded to nearest, ties to even, as NumPy's astype), 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 D2 source · line 87 · raw
Data
D2@hi:U32 -> @lo:U32 -> D2
type Tok source · line 219 · raw
Data
TStr@s:List<&2, U32> -> Tok
TWord@s:List<&2, U32> -> Tok
TNum@n:Nat -> Tok
TMark@c:U32 -> Tok
type Lex source · line 225 · raw
Data
LIdleLex
LStr@q:U32 -> @acc:List<&2, U32> -> Lex
LWord@acc:List<&2, U32> -> Lex
LNum@n:Nat -> Lex
type LexT source · line 244 · raw
Data
the state after one byte from idle, and the token list
LexT@st:Lex -> @out:List<&2, Tok> -> LexT
type Hdr source · line 348 · raw
Data
Hdr@descr:List<&2, U32> -> @fortran:Bool -> @shape:List<&2, Nat> -> Hdr
type Elt source · line 365 · 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
Elt@kd:U32 -> @size:Nat -> @big:Bool -> @dt:0xcc7fe2a980487676abb11c4601fc801f/shape.DType -> Elt
type Mode source · line 416 · raw
Data
how one stored element becomes a cell, decided once per file
MF16Mode
MF32Mode
MF64Mode
MI64Mode
MU64Mode
MI8Mode
MI16Mode
MU16Mode
MByteMode
MBoolMode
MWordMode
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 shr64 source · line 90 · raw
@+hi:U32 -> @+lo:U32 -> @+n:Nat -> D2
def shl64 source · line 94 · raw
@+hi:U32 -> @+lo:U32 -> @+n:Nat -> D2
def sub64 source · line 98 · raw
@a:D2 -> @b:D2 -> D2
def lt64 source · line 103 · raw
@a:D2 -> @b:D2 -> Bool
def eq64 source · line 108 · raw
@a:D2 -> @b:D2 -> Bool
def lo_of source · line 113 · raw
@d:D2 -> U32
def round_up source · line 118 · raw
@+q:U32 -> @+rem:D2 -> @+half:D2 -> U32
def round_q source · line 122 · raw
@+q:U32 -> @+hi:U32 -> @+lo:U32 -> @+s:Nat -> U32
the 53-bit significand (hi, lo) shifted right by s (29 .. 63), rounded to nearest, ties to even
def round_shift source · line 125 · raw
@+hi:U32 -> @+lo:U32 -> @+s:Nat -> U32
def f64_finite source · line 130 · raw
@+sign:U32 -> @+e:U32 -> @+mh:U32 -> @+lo:U32 -> U32
e the biased exponent (1 .. 2046); 897 .. 1150 are float32's normal range (U32 throughout: the checker keeps a Nat unary, so exponents and fields stay words, and only the shift counts below 64 are Nats)
def f64_parts source · line 137 · raw
@+sign:U32 -> @+e:U32 -> @+mh:U32 -> @+lo:U32 -> U32
float64 (its low and high words) to float32 bits
def f64_to_f32 source · line 145 · raw
@+lo:U32 -> @+hi:U32 -> U32
def top_bit source · line 149 · raw
@fuel:Nat -> @+m:U32 -> @+p:Nat -> Nat
the highest set bit of m (1 .. 1023), counted from 0
def f16_sub source · line 156 · raw
@+sign:U32 -> @+m:U32 -> @+p:Nat -> U32
def f16_parts source · line 159 · raw
@+sign:U32 -> @+e:U32 -> @+m:U32 -> U32
def f16_to_f32 source · line 167 · raw
@+h:U32 -> U32
def descr source · line 170 · raw
@dt:0xcc7fe2a980487676abb11c4601fc801f/shape.DType -> String
def wide source · line 183 · raw
@dt:0xcc7fe2a980487676abb11c4601fc801f/shape.DType -> Bool
def header_text source · line 193 · raw
@+dt:0xcc7fe2a980487676abb11c4601fc801f/shape.DType -> @+s:List<&2, Nat> -> List<&2, U32>
the header dict as np.save writes it
def header_padded source · line 197 · 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 200 · raw
@w:Bool -> @cells:List<&2, U32> -> List<&2, U32>
def file_bytes source · line 207 · raw
@+dt:0xcc7fe2a980487676abb11c4601fc801f/shape.DType -> @+h:List<&2, U32> -> @cells:List<&2, U32> -> List<&2, U32>
def to_npy source · line 212 · raw
@a:0xcc7fe2a980487676abb11c4601fc801f/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 231 · raw
@+b:U32 -> Bool
def is_alpha source · line 234 · raw
@+b:U32 -> Bool
def is_quote source · line 237 · raw
@+b:U32 -> Bool
def is_mark source · line 240 · raw
@+b:U32 -> Bool
def idle_byte source · line 247 · raw
@+b:U32 -> @+out:List<&2, Tok> -> LexT
def lex_step source · line 253 · raw
@l:LexT -> @+b:U32 -> LexT
def lex_go source · line 264 · raw
@bs:List<&2, U32> -> @l:LexT -> LexT
def lex_end source · line 271 · raw
@l:LexT -> List<&2, Tok>
def lex source · line 281 · raw
@bs:List<&2, U32> -> List<&2, Tok>
the header's tokens: strings, words, numbers and the marks { } ( ) : ,
def bytes_eq source · line 284 · raw
@a:List<&2, U32> -> @b:List<&2, U32> -> Bool
def is_key source · line 293 · raw
@t:Tok -> @+k:List<&2, U32> -> Bool
def bdrop_tok source · line 301 · raw
@ts:List<&2, Tok> -> List<&2, Tok>
the tokens after the key k and its colon
def after_key source · line 308 · raw
@ts:List<&2, Tok> -> @+k:List<&2, U32> -> List<&2, Tok>
def str_val source · line 315 · raw
@ts:List<&2, Tok> -> Maybe<&2, List<&2, U32>>
def bool_val source · line 322 · raw
@ts:List<&2, Tok> -> Maybe<&2, Bool>
def nums_go source · line 330 · 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 341 · raw
@ts:List<&2, Tok> -> Maybe<&2, List<&2, Nat>>
def hdr_of source · line 351 · raw
@d:Maybe<&2, List<&2, U32>> -> @f:Maybe<&2, Bool> -> @s:Maybe<&2, List<&2, Nat>> -> Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, Hdr>
def header source · line 359 · raw
@+ts:List<&2, Tok> -> Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, Hdr>
the header dict's three entries
def kind_of source · line 368 · raw
@+k:U32 -> @+n:Nat -> @+big:Bool -> Maybe<&2, Elt>
def num_go source · line 380 · 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 387 · raw
@bs:List<&2, U32> -> Nat
def parse_descr source · line 390 · raw
@d:List<&2, U32> -> Maybe<&2, Elt>
def ordered source · line 399 · raw
@+big:Bool -> @+bs:List<&2, U32> -> List<&2, U32>
the element's bytes, lowest first
def w4 source · line 402 · raw
@+bs:List<&2, U32> -> U32
def w2 source · line 405 · raw
@+bs:List<&2, U32> -> U32
def sext source · line 408 · raw
@+x:U32 -> @+bits:U32 -> @+n:Nat -> U32
def wide_int source · line 412 · 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 429 · raw
@k:Elt -> Mode
def elem source · line 438 · raw
@m:Mode -> @+bs:List<&2, U32> -> Maybe<&2, U32>
one element (its bytes lowest first) as a cell, or None when it does not fit
def elem_got source · line 463 · raw
@r:Maybe<&2, U32> -> @+i:Nat -> @acc:List<&2, U32> -> @+name:String -> @more:(@_:Unit -> @_:List<&2, U32> -> Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, List<&2, U32>>) -> Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, List<&2, U32>>
def elems_go source · line 471 · raw
@n:Nat -> @+k:Mode -> @+size:Nat -> @+big:Bool -> @+i:Nat -> @+bs:List<&2, U32> -> @acc:List<&2, U32> -> @+name:String -> Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, List<&2, U32>>
def kind_size source · line 478 · raw
@k:Elt -> Nat
def kind_big source · line 483 · raw
@k:Elt -> Bool
def kind_dt source · line 488 · raw
@k:Elt -> 0xcc7fe2a980487676abb11c4601fc801f/shape.DType
def fortran_fix source · line 495 · raw
@f:Bool -> @a:0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND -> 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND
def shaped source · line 502 · raw
@r:Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, List<&2, U32>> -> @+dt:0xcc7fe2a980487676abb11c4601fc801f/shape.DType -> @+s:List<&2, Nat> -> @+fortran:Bool -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def enough source · line 510 · raw
@+need:Nat -> @+have:Nat -> Bool
a Fortran-order array is stored as its transpose in C order
def kind_go source · line 513 · raw
@ok:Bool -> @+k:Elt -> @+d:List<&2, U32> -> @+f:Bool -> @+s:List<&2, Nat> -> @+data:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def with_kind source · line 520 · raw
@m:Maybe<&2, Elt> -> @h:Hdr -> @+data:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def hdr_descr source · line 527 · raw
@h:Hdr -> List<&2, U32>
def with_header source · line 532 · raw
@r:Result<&2, &2, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, Hdr> -> @+data:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def body source · line 539 · raw
@+start:Nat -> @+hlen:Nat -> @+bs:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def v_two source · line 542 · raw
@ok:Bool -> @+v:U32 -> @+bs:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def v_one source · line 549 · raw
@is1:Bool -> @+v:U32 -> @+bs:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def magic_ok source · line 556 · raw
@m:Bool -> @+bs:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
def from_npy source · line 564 · raw
@+bs:List<&2, U32> -> Result<&1, &1, 0xcc7fe2a980487676abb11c4601fc801f/shape.NdErr, 0xcc7fe2a980487676abb11c4601fc801f/ndarray.ND>
the array in a .npy file's bytes