Module: Numpy

Defined in:
lib/dnn/numo2numpy.rb

Class Method Summary collapse

Class Method Details

.from_na(narray) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dnn/numo2numpy.rb', line 15

def self.from_na(narray)
  bin = narray.to_binary
  bin.force_encoding("ASCII-8BIT")
  case
  when narray.is_a?(Numo::Int8)
    Numpy.frombuffer(bin, dtype: "int8").reshape(*narray.shape)
  when narray.is_a?(Numo::UInt8)
    Numpy.frombuffer(bin, dtype: "uint8").reshape(*narray.shape)
  when narray.is_a?(Numo::Int16)
    Numpy.frombuffer(bin, dtype: "int16").reshape(*narray.shape)
  when narray.is_a?(Numo::UInt16)
    Numpy.frombuffer(bin, dtype: "uint16").reshape(*narray.shape)
  when narray.is_a?(Numo::Int32)
    Numpy.frombuffer(bin, dtype: "int32").reshape(*narray.shape)
  when narray.is_a?(Numo::UInt32)
    Numpy.frombuffer(bin, dtype: "uint32").reshape(*narray.shape)
  when narray.is_a?(Numo::Int64)
    Numpy.frombuffer(bin, dtype: "int64").reshape(*narray.shape)
  when narray.is_a?(Numo::UInt64)
    Numpy.frombuffer(bin, dtype: "uint64").reshape(*narray.shape)
  when narray.is_a?(Numo::SFloat)
    Numpy.frombuffer(bin, dtype: "float32").reshape(*narray.shape)
  when narray.is_a?(Numo::DFloat)
    Numpy.frombuffer(bin, dtype: "float64").reshape(*narray.shape)
  else
    raise NumpyToNumoError.new("#{narray.class.name} is not support convert.")
  end
end

.to_na(ndarray) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dnn/numo2numpy.rb', line 44

def self.to_na(ndarray)
  shape = ndarray.shape
  bin = ndarray.flatten.tobytes
  case ndarray.dtype.to_s
  when "int8"
    Numo::Int8.from_binary(bin).reshape(*shape)
  when "uint8"
    Numo::UInt8.from_binary(bin).reshape(*shape)
  when "int16"
    Numo::Int16.from_binary(bin).reshape(*shape)
  when "uint16"
    Numo::UInt16.from_binary(bin).reshape(*shape)
  when "int32"
    Numo::Int32.from_binary(bin).reshape(*shape)
  when "uint32"
    Numo::UInt32.from_binary(bin).reshape(*shape)
  when "int64"
    Numo::Int64.from_binary(bin).reshape(*shape)
  when "uint64"
    Numo::UInt64.from_binary(bin).reshape(*shape)
  when "float32"
    Numo::SFloat.from_binary(bin).reshape(*shape)
  when "float64"
    Numo::DFloat.from_binary(bin).reshape(*shape)
  else
    raise NumpyToNumoError.new("#{ndarray.dtype} is not support convert.")
  end
end