npy 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +10 -10
  4. data/lib/npy.rb +77 -27
  5. data/lib/npy/version.rb +1 -1
  6. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e81a0b6a617c8f05e1e62f88c05996891328ef154cd4c60d49d233b41b99748
4
- data.tar.gz: 4f8c8cc599e2bae978b1cc6dabcadb2dd6aab1dec96c1f5f3e07a8a4303f1a24
3
+ metadata.gz: 3d65145c08fd1a4b752d272bfdd1a68495637eabb29092b1adb090f77d32abb2
4
+ data.tar.gz: bd7fd9b4bb57ad565dc3a368e1fdeeb23481196838ed5deb84a2dd153f303998
5
5
  SHA512:
6
- metadata.gz: 82babeb75230ec73cb0870b42716589bc2bf9a460ff9943606792b9fe3dfa941f5892c4de1ba7b719afe2fa0e3726b2f5ca5ca268224afe2273c5c66af881cba
7
- data.tar.gz: 700466088d563a0a8f4ccebce9506d91675b41bc1f47525756637b49218c533bfeef5364f3dea5b71328d690589643947273db00b44b7af8f86a77acdaf59ce3
6
+ metadata.gz: fd14323a074d5aef907ebc3c8457ff5301cd8b612f362901676d2e114668cb2d998e84fa17896a444590ba397337adfc5d761883c82c4a850ac479f1efde8801
7
+ data.tar.gz: 61eedeea139e6e0d086f18777becb33b0f0d5272655d401e46241fa215ac9b9f6dacf943e477273b72c9a812415f13fb35727f7bc26cc95508bc2d823aad7070
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.1.2
2
+
3
+ - Added support for saving and loading IO objects
4
+ - Added `load_npz_string`
5
+ - Fixed bug with 1 dimensional arrays
6
+
1
7
  ## 0.1.1
2
8
 
3
9
  - Added support for saving files
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Save and load NumPy `npy` and `npz` files in Ruby - no Python required
4
4
 
5
- :fire: Uses [Numo::NArray](https://github.com/ruby-numo/numo-narray) for blazing performance
5
+ :fire: Uses [Numo](https://github.com/ruby-numo/numo-narray) for blazing performance
6
6
 
7
7
  [![Build Status](https://travis-ci.org/ankane/npy.svg?branch=master)](https://travis-ci.org/ankane/npy)
8
8
 
@@ -23,21 +23,21 @@ gem 'npy'
23
23
  Save an array
24
24
 
25
25
  ```ruby
26
- arr = Numo::Int32[0...10]
27
- Npy.save("x.npy", arr)
26
+ x = Numo::Int32[0..9]
27
+ Npy.save("x.npy", x)
28
28
  ```
29
29
 
30
30
  Load an `npy` file
31
31
 
32
32
  ```ruby
33
- arr = Npy.load("x.npy")
33
+ x = Npy.load("x.npy")
34
34
  ```
35
35
 
36
36
  Load an `npy` string
37
37
 
38
38
  ```ruby
39
39
  byte_str = File.binread("x.npy")
40
- arr = Npy.load_string(byte_str)
40
+ x = Npy.load_string(byte_str)
41
41
  ```
42
42
 
43
43
  ### npz
@@ -47,27 +47,27 @@ arr = Npy.load_string(byte_str)
47
47
  Save multiple arrays
48
48
 
49
49
  ```ruby
50
- x = Numo::Int32[0...10]
50
+ x = Numo::Int32[0..9]
51
51
  y = x * 2
52
- Npy.save_npz("mnist.npz", x: x, y: y)
52
+ Npy.save_npz("data.npz", x: x, y: y)
53
53
  ```
54
54
 
55
55
  Load an `npz` file
56
56
 
57
57
  ```ruby
58
- arrs = Npy.load_npz("mnist.npz")
58
+ data = Npy.load_npz("data.npz")
59
59
  ```
60
60
 
61
61
  Get keys
62
62
 
63
63
  ```ruby
64
- arrs.keys
64
+ data.keys
65
65
  ```
66
66
 
67
67
  Get an array
68
68
 
69
69
  ```ruby
70
- arrs["x"]
70
+ data["x"]
71
71
  ```
72
72
 
73
73
  Arrays are lazy loaded for performance
data/lib/npy.rb CHANGED
@@ -2,6 +2,10 @@
2
2
  require "numo/narray"
3
3
  require "zip"
4
4
 
5
+ # stdlib
6
+ require "stringio"
7
+ require "tempfile"
8
+
5
9
  # modules
6
10
  require "npy/file"
7
11
  require "npy/version"
@@ -28,14 +32,20 @@ module Npy
28
32
 
29
33
  class << self
30
34
  def load(path)
31
- with_file(path) do |f|
32
- load_io(f)
35
+ case path
36
+ when IO, StringIO
37
+ load_io(path)
38
+ else
39
+ load_file(path)
33
40
  end
34
41
  end
35
42
 
36
43
  def load_npz(path)
37
- with_file(path) do |f|
38
- load_npz_io(f)
44
+ case path
45
+ when IO, StringIO
46
+ load_npz_io(path)
47
+ else
48
+ load_npz_file(path)
39
49
  end
40
50
  end
41
51
 
@@ -43,11 +53,19 @@ module Npy
43
53
  load_io(StringIO.new(byte_str))
44
54
  end
45
55
 
46
- # rubyzip not playing nicely with StringIO
47
- # def load_npz_string(byte_str)
48
- # load_npz_io(StringIO.new(byte_str))
49
- # end
56
+ def load_npz_string(byte_str)
57
+ # not playing nicely with StringIO
58
+ file = Tempfile.new("npy")
59
+ begin
60
+ file.write(byte_str)
61
+ load_npz_io(file)
62
+ ensure
63
+ file.close
64
+ file.unlink
65
+ end
66
+ end
50
67
 
68
+ # TODO make private
51
69
  def load_io(io)
52
70
  magic = io.read(6)
53
71
  raise Error, "Invalid npy format" unless magic&.b == MAGIC_STR
@@ -81,50 +99,66 @@ module Npy
81
99
  result
82
100
  end
83
101
 
102
+ # TODO make private
84
103
  def load_npz_io(io)
85
104
  File.new(io)
86
105
  end
87
106
 
88
107
  def save(path, arr)
89
- ::File.open(path, "wb") do |f|
90
- save_io(f, arr)
108
+ case path
109
+ when IO, StringIO
110
+ save_io(path, arr)
111
+ else
112
+ save_file(path, arr)
91
113
  end
92
114
  true
93
115
  end
94
116
 
95
- def save_npz(path, **arrs)
96
- # use File.open instead passing path to zip file
97
- # so it overrides instead of appends
98
- ::File.open(path, "wb") do |f|
99
- Zip::File.open(f, Zip::File::CREATE) do |zipfile|
100
- arrs.each do |k, v|
101
- zipfile.get_output_stream("#{k}.npy") do |f2|
102
- save_io(f2, v)
103
- end
104
- end
105
- end
117
+ def save_npz(path, arrs)
118
+ case path
119
+ when IO, StringIO
120
+ save_npz_io(path, arrs)
121
+ else
122
+ save_npz_file(path, arrs)
106
123
  end
107
124
  true
108
125
  end
109
126
 
110
127
  private
111
128
 
129
+ def load_file(path)
130
+ with_file(path, "rb") do |f|
131
+ load_io(f)
132
+ end
133
+ end
134
+
135
+ def load_npz_file(path)
136
+ with_file(path, "rb") do |f|
137
+ load_npz_io(f)
138
+ end
139
+ end
140
+
141
+ def save_file(path, arr)
142
+ with_file(path, "wb") do |f|
143
+ save_io(f, arr)
144
+ end
145
+ end
146
+
112
147
  def save_io(f, arr)
113
148
  empty_shape = arr.is_a?(Numeric)
114
149
  arr = Numo::NArray.cast([arr]) if empty_shape
115
150
  arr = Numo::NArray.cast(arr) if arr.is_a?(Array)
116
151
 
117
152
  # desc
118
- descr = TYPE_MAP.find { |k, v| arr.is_a?(v) }
153
+ descr = TYPE_MAP.find { |_, v| arr.is_a?(v) }
119
154
  raise Error, "Unsupported type: #{arr.class.name}" unless descr
120
155
 
121
156
  # shape
122
157
  shape = arr.shape
123
- shape << "" if shape.size == 1
124
158
  shape = [] if empty_shape
125
159
 
126
160
  # header
127
- header = "{'descr': '#{descr[0]}', 'fortran_order': False, 'shape': (#{shape.join(", ")}), }".b
161
+ header = "{'descr': '#{descr[0]}', 'fortran_order': False, 'shape': (#{shape.join(", ")}#{shape.size == 1 ? "," : nil}), }".b
128
162
  padding_len = 64 - (11 + header.length) % 64
129
163
  padding = "\x20".b * padding_len
130
164
  header = "#{header}#{padding}\n"
@@ -136,8 +170,24 @@ module Npy
136
170
  f.write(arr.to_string)
137
171
  end
138
172
 
139
- def with_file(path)
140
- ::File.open(path, "rb") do |f|
173
+ def save_npz_file(path, arrs)
174
+ with_file(path, "wb") do |f|
175
+ save_npz_io(f, arrs)
176
+ end
177
+ end
178
+
179
+ def save_npz_io(f, arrs)
180
+ Zip::File.open(f, Zip::File::CREATE) do |zipfile|
181
+ arrs.each do |k, v|
182
+ zipfile.get_output_stream("#{k}.npy") do |f2|
183
+ save_io(f2, v)
184
+ end
185
+ end
186
+ end
187
+ end
188
+
189
+ def with_file(path, mode)
190
+ ::File.open(path, mode) do |f|
141
191
  yield f
142
192
  end
143
193
  end
@@ -158,7 +208,7 @@ module Npy
158
208
  # shape
159
209
  m = /'shape': *\(([^)]*)\)/.match(header)
160
210
  # no space in split for max compatibility
161
- shape = m[1].split(",").map(&:to_i)
211
+ shape = m[1].strip.split(",").map(&:to_i)
162
212
 
163
213
  [descr, fortran_order, shape]
164
214
  end
data/lib/npy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Npy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: npy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-14 00:00:00.000000000 Z
11
+ date: 2019-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: benchmark-ips
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email: andrew@chartkick.com
85
99
  executables: []