bqn-npy/npy.bqn

45 lines
1.8 KiB
BQN
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

SaveNpy,LoadNpy
lf@+10
magicString(@+147)"NUMPY"
# Type conversions
dtypesformats"<f8","<i4","<u4"64'f',32'i',32'u'
DtypeToFormat{(dtypes<𝕩)formats}
ArrayToBytes{DtypeToFormat 𝕨,8'c'•bit._cast 𝕩}
ArrayFromBytes{8'c',DtypeToFormat 𝕨•bit._cast 𝕩}
# Saving
To_i16le{@+256(|÷˜)𝕩}
FormatShape{'('(1','˘•Fmt¨𝕩)')'}
BuildHeader{dtype 𝕊 data:
version10
# Hypothesis: BQN arrays are C-contiguous
headerData"{'descr':'"dtype"','fortran_order':False,'shape':"(FormatShape data)",}"
padding' '˜64-64|(magicString)+2+2+(headerData)+1
! 0=64|(magicString)+2+2+(padding)+(headerData)+1
headerDataPaddedheaderDatapaddinglf
magicString(@+version)(To_i16le headerDataPadded)headerDataPadded
}
DetectDtype{(´=¨𝕩)"<f8",(´0𝕩)"<i4""<u4"}
EncodeNpyDetectDtype(BuildHeaderArrayToBytes)
SaveNpy•file.BytesEncodeNpy
# Loading
ParseHeader{𝕊𝕩:
"Not a valid NPY file"! magicString(magicString)𝕩
version@-˜(01+magicString)𝕩
headerlen+´1256×@-˜(23+magicString)𝕩
header(¬(' 'lf))/headerlen(4+magicString)𝕩
dtype3('<') (("descr"header)1)header
shapestr(')') (1+'(') (("shape"header)1)header
shape•BQN¨','((-˜+`׬)=)shapestr
version,dtype,shape,headerlen+2+2+magicString
}
LoadNpy{𝕊𝕩:
bytes•file.Bytes 𝕩
versiondtypeshapedatastartParseHeader bytes
shapedtype ArrayFromBytes datastartbytes
}