# dependencies require "numo/narray" require "zip" # modules require "npy/file" require "npy/version" module Npy class Error < StandardError; end MAGIC_STR = "\x93NUMPY".b class << self def load(path) with_file(path) do |f| load_io(f) end end def load_npz(path) with_file(path) do |f| load_npz_io(f) end end def load_string(byte_str) load_io(StringIO.new(byte_str)) end # rubyzip not playing nicely with StringIO # def load_npz_string(byte_str) # load_npz_io(StringIO.new(byte_str)) # end def load_io(io) magic = io.read(6) raise Error, "Invalid npy format" unless magic&.b == MAGIC_STR major_version = io.read(1) minor_version = io.read(1) raise Error, "Unsupported version" unless major_version == "\x01".b header_len = io.read(2).unpack1("S<") header = io.read(header_len) descr, fortran_order, shape = parse_header(header) raise Error, "Fortran order not supported" if fortran_order klass = case descr when "|i1" Numo::Int8 when "