Module: Npy

Defined in:
lib/npy.rb,
lib/npy/file.rb,
lib/npy/version.rb

Defined Under Namespace

Classes: Error, File

Constant Summary collapse

MAGIC_STR =
"\x93NUMPY".b
TYPE_MAP =
{
  "|i1" => Numo::Int8,
  "<i2" => Numo::Int16,
  "<i4" => Numo::Int32,
  "<i8" => Numo::Int64,
  "|u1" => Numo::UInt8,
  "<u2" => Numo::UInt16,
  "<u4" => Numo::UInt32,
  "<u8" => Numo::UInt64,
  "<f4" => Numo::SFloat,
  "<f8" => Numo::DFloat,
  "<c8" => Numo::SComplex,
  "<c16" => Numo::DComplex,
  # must come last
  # as save uses first match
  "|b1" => Numo::UInt8
}
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/npy.rb', line 37

def load(path)
  case path
  when IO, StringIO
    load_io(path)
  else
    load_file(path)
  end
end

.load_io(io) ⇒ Object

TODO make private

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/npy.rb', line 72

def load_io(io)
  magic = io.read(6)
  raise Error, "Invalid npy format" unless magic&.b == MAGIC_STR

  version = io.read(2)

  header_len =
    case version
    when "\x01\x00".b
      io.read(2).unpack1("S<")
    when "\x02\x00".b, "\x03\x00".b
      io.read(4).unpack1("I<")
    else
      raise Error, "Unsupported version"
    end
  header = io.read(header_len)
  descr, fortran_order, shape = parse_header(header)
  raise Error, "Fortran order not supported" if fortran_order

  klass = TYPE_MAP[descr]
  raise Error, "Type not supported: #{descr}" unless klass

  # use from_string instead of from_binary for max compatibility
  # from_binary introduced in 0.9.0.4
  # numo from_string can't handle rank0
  if shape.empty?
    klass.cast(klass.from_string(io.read, [1])[0])
  else
    klass.from_string(io.read, shape)
  end
end

.load_npz(path) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/npy.rb', line 46

def load_npz(path)
  case path
  when IO, StringIO
    load_npz_io(path)
  else
    load_npz_file(path)
  end
end

.load_npz_io(io) ⇒ Object

TODO make private



105
106
107
# File 'lib/npy.rb', line 105

def load_npz_io(io)
  File.new(io)
end

.load_npz_string(byte_str) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/npy.rb', line 59

def load_npz_string(byte_str)
  # not playing nicely with StringIO
  file = Tempfile.new("npy")
  begin
    file.write(byte_str)
    load_npz_io(file)
  ensure
    file.close
    file.unlink
  end
end

.load_string(byte_str) ⇒ Object



55
56
57
# File 'lib/npy.rb', line 55

def load_string(byte_str)
  load_io(StringIO.new(byte_str))
end

.save(path, arr) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/npy.rb', line 109

def save(path, arr)
  case path
  when IO, StringIO
    save_io(path, arr)
  else
    save_file(path, arr)
  end
  true
end

.save_npz(path, arrs) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/npy.rb', line 119

def save_npz(path, arrs)
  case path
  when IO, StringIO
    save_npz_io(path, arrs)
  else
    save_npz_file(path, arrs)
  end
  true
end