Module: Numo::Binrw

Defined in:
lib/numo/binrw.rb,
lib/numo/binrw/version.rb,
ext/numo/binrw/binrw.c

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

._bin_read(cls, filename) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/numo/binrw/read.c', line 32

VALUE nrw_bin_read(VALUE self, VALUE cls, VALUE filename){
    long size = NUM2LONG(rb_funcall(rb_funcall(rb_cFile, rb_intern("stat"), 1, filename), rb_intern("size"), 0));
    struct bin_read_arg_ret st;
    int ssize;
    if(cls == numo_cDFloat){
        ssize = 8;
    }else if(cls == numo_cSFloat){
        ssize = 4;
    }else if(cls == numo_cInt32){
        ssize = 4;
    }else if(cls == numo_cInt64){
        ssize = 8;
    }else{
        return Qfalse; // 

._bin_writeObject

.bin_read(cls, filename) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/numo/binrw.rb', line 31

def self.bin_read(cls, filename)
    if filename.is_a? Array
        unless filename.map{|file| File.stat(file).size}.uniq.size == 1
            raise "The size of all data must be the same"
        end
        row = filename.size
        size = File.stat(filename[0]).size
        if cls == DFloat || cls == Int64
            col = size / 8
        elsif cls == SFloat || cls == Int32
            col = size / 4
        else
            raise "Unsupported class"
        end
        a = cls.zeros(row, col)
        filename.each.with_index do |file, i|
            a[i, true] = bin_read(cls, file)
        end
        return a
    end

    Numo::Binrw._bin_read(cls, filename)
end

.bin_write(obj, filename) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/numo/binrw.rb', line 12

def self.bin_write(obj, filename)
    if filename.is_a? Array
        if obj.ndim != 2
            # 複数行う場合は行列
            raise "If multiple files are specified, it must be a matrix"
        end
        if obj.shape[0] != filename.size
            # ファイル数と行数は同じ
            raise "If multiple files are specified, the number of files and the number of lines must be the same"
        end
    elsif filename.is_a? String
    else
        raise
    end

    Numo::Binrw._bin_write(obj, filename)
    obj
end