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
}
VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/npy.rb', line 34

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:



69
70
71
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
# File 'lib/npy.rb', line 69

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

  # numo can't handle empty shapes
  empty_shape = shape.empty?
  shape = [1] if empty_shape

  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
  result = klass.from_string(io.read, shape)
  result = result[0] if empty_shape
  result
end

.load_npz(path) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/npy.rb', line 43

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



103
104
105
# File 'lib/npy.rb', line 103

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

.load_npz_string(byte_str) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/npy.rb', line 56

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



52
53
54
# File 'lib/npy.rb', line 52

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

.save(path, arr) ⇒ Object



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

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



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

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