npy 0.4.0 → 1.0.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/LICENSE.txt +1 -1
- data/lib/npy/file.rb +3 -3
- data/lib/npy/version.rb +1 -1
- data/lib/npy.rb +50 -68
- metadata +9 -13
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 81818d5fb5b2ce45fd024806af7f8974d34bcd66c951ea1a22e966c42d767e4f
|
|
4
|
+
data.tar.gz: 95e10ae07dfac61ee6f8596e53e3600d4efb797ba7998ab1c2ee9cba8551cc3c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 77ffad380f7e7ce64e9611a50e756203baebc34bd7d649089288c959f1196cdfb2e99d1666204c2f14e8318af7143ffae4dff99f3a6f0f2aad006f444b0819d8
|
|
7
|
+
data.tar.gz: 0aff2a43fbba4917f13f3c59333bc02814c411f7b25880dc3f1286d98ac36c76c6bfb9ee34d5899c4f3745c816187dd1bf8e77b91cf3f54485c53a9bf13fa2f5
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/lib/npy/file.rb
CHANGED
|
@@ -4,8 +4,8 @@ module Npy
|
|
|
4
4
|
@streams = {}
|
|
5
5
|
Zip::File.open_buffer(io) do |zipfile|
|
|
6
6
|
zipfile.each do |entry|
|
|
7
|
-
name = entry.name.
|
|
8
|
-
@streams[name] = entry
|
|
7
|
+
name = entry.name.delete_suffix(".npy")
|
|
8
|
+
@streams[name] = entry
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
11
|
@data = {}
|
|
@@ -16,7 +16,7 @@ module Npy
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def [](name)
|
|
19
|
-
@data[name] ||= Npy.
|
|
19
|
+
@data[name] ||= Npy.load(@streams[name].get_input_stream) if @streams[name]
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def to_h
|
data/lib/npy/version.rb
CHANGED
data/lib/npy.rb
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
# dependencies
|
|
2
|
-
require "numo/narray"
|
|
2
|
+
require "numo/narray/alt"
|
|
3
3
|
require "zip"
|
|
4
4
|
|
|
5
5
|
# stdlib
|
|
6
6
|
require "stringio"
|
|
7
|
-
require "tempfile"
|
|
8
7
|
|
|
9
8
|
# modules
|
|
10
9
|
require_relative "npy/file"
|
|
@@ -34,21 +33,19 @@ module Npy
|
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
class << self
|
|
37
|
-
def load(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
load_io(path)
|
|
36
|
+
def load(file)
|
|
37
|
+
if file.respond_to?(:read)
|
|
38
|
+
load_io(file)
|
|
41
39
|
else
|
|
42
|
-
load_file(
|
|
40
|
+
load_file(file)
|
|
43
41
|
end
|
|
44
42
|
end
|
|
45
43
|
|
|
46
|
-
def load_npz(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
load_npz_io(path)
|
|
44
|
+
def load_npz(file)
|
|
45
|
+
if file.respond_to?(:read)
|
|
46
|
+
load_npz_io(file)
|
|
50
47
|
else
|
|
51
|
-
load_npz_file(
|
|
48
|
+
load_npz_file(file)
|
|
52
49
|
end
|
|
53
50
|
end
|
|
54
51
|
|
|
@@ -57,18 +54,35 @@ module Npy
|
|
|
57
54
|
end
|
|
58
55
|
|
|
59
56
|
def load_npz_string(byte_str)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
file
|
|
57
|
+
load_npz_io(StringIO.new(byte_str))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def save(file, arr)
|
|
61
|
+
if file.respond_to?(:write)
|
|
62
|
+
save_io(file, arr)
|
|
63
|
+
else
|
|
64
|
+
save_file(file, arr)
|
|
65
|
+
end
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def save_npz(file, arrs)
|
|
70
|
+
if file.respond_to?(:write)
|
|
71
|
+
save_npz_io(file, arrs)
|
|
72
|
+
else
|
|
73
|
+
save_npz_file(file, arrs)
|
|
74
|
+
end
|
|
75
|
+
true
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def load_file(path)
|
|
81
|
+
with_file(path, "rb") do |f|
|
|
82
|
+
load_io(f)
|
|
68
83
|
end
|
|
69
84
|
end
|
|
70
85
|
|
|
71
|
-
# TODO make private
|
|
72
86
|
def load_io(io)
|
|
73
87
|
magic = io.read(6)
|
|
74
88
|
raise Error, "Invalid npy format" unless magic&.b == MAGIC_STR
|
|
@@ -91,46 +105,11 @@ module Npy
|
|
|
91
105
|
klass = TYPE_MAP[descr]
|
|
92
106
|
raise Error, "Type not supported: #{descr}" unless klass
|
|
93
107
|
|
|
94
|
-
# use from_string instead of from_binary for max compatibility
|
|
95
|
-
# from_binary introduced in 0.9.0.4
|
|
96
108
|
# numo from_string can't handle rank0
|
|
97
109
|
if shape.empty?
|
|
98
110
|
klass.cast(klass.from_string(io.read, [1])[0])
|
|
99
111
|
else
|
|
100
|
-
klass.
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# TODO make private
|
|
105
|
-
def load_npz_io(io)
|
|
106
|
-
File.new(io)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def save(path, arr)
|
|
110
|
-
case path
|
|
111
|
-
when IO, StringIO
|
|
112
|
-
save_io(path, arr)
|
|
113
|
-
else
|
|
114
|
-
save_file(path, arr)
|
|
115
|
-
end
|
|
116
|
-
true
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def save_npz(path, arrs)
|
|
120
|
-
case path
|
|
121
|
-
when IO, StringIO
|
|
122
|
-
save_npz_io(path, arrs)
|
|
123
|
-
else
|
|
124
|
-
save_npz_file(path, arrs)
|
|
125
|
-
end
|
|
126
|
-
true
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
private
|
|
130
|
-
|
|
131
|
-
def load_file(path)
|
|
132
|
-
with_file(path, "rb") do |f|
|
|
133
|
-
load_io(f)
|
|
112
|
+
klass.from_binary(io.read, shape)
|
|
134
113
|
end
|
|
135
114
|
end
|
|
136
115
|
|
|
@@ -140,13 +119,17 @@ module Npy
|
|
|
140
119
|
end
|
|
141
120
|
end
|
|
142
121
|
|
|
122
|
+
def load_npz_io(io)
|
|
123
|
+
File.new(io)
|
|
124
|
+
end
|
|
125
|
+
|
|
143
126
|
def save_file(path, arr)
|
|
144
127
|
with_file(path, "wb") do |f|
|
|
145
128
|
save_io(f, arr)
|
|
146
129
|
end
|
|
147
130
|
end
|
|
148
131
|
|
|
149
|
-
def save_io(
|
|
132
|
+
def save_io(io, arr)
|
|
150
133
|
unless arr.is_a?(Numo::NArray)
|
|
151
134
|
begin
|
|
152
135
|
arr = Numo::NArray.cast(arr)
|
|
@@ -168,11 +151,11 @@ module Npy
|
|
|
168
151
|
padding = "\x20".b * padding_len
|
|
169
152
|
header = "#{header}#{padding}\n"
|
|
170
153
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
154
|
+
io.write(MAGIC_STR)
|
|
155
|
+
io.write("\x01\x00".b)
|
|
156
|
+
io.write([header.bytesize].pack("S<"))
|
|
157
|
+
io.write(header)
|
|
158
|
+
io.write(arr.to_binary)
|
|
176
159
|
end
|
|
177
160
|
|
|
178
161
|
def save_npz_file(path, arrs)
|
|
@@ -181,12 +164,11 @@ module Npy
|
|
|
181
164
|
end
|
|
182
165
|
end
|
|
183
166
|
|
|
184
|
-
def save_npz_io(
|
|
185
|
-
|
|
186
|
-
Zip::File.open(f, true) do |zipfile|
|
|
167
|
+
def save_npz_io(io, arrs)
|
|
168
|
+
Zip::File.open(io, create: true) do |zipfile|
|
|
187
169
|
arrs.each do |k, v|
|
|
188
|
-
zipfile.get_output_stream("#{k}.npy") do |
|
|
189
|
-
save_io(
|
|
170
|
+
zipfile.get_output_stream("#{k}.npy") do |f|
|
|
171
|
+
save_io(f, v)
|
|
190
172
|
end
|
|
191
173
|
end
|
|
192
174
|
end
|
metadata
CHANGED
|
@@ -1,44 +1,42 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: npy
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrew Kane
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: numo-narray
|
|
13
|
+
name: numo-narray-alt
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '0'
|
|
18
|
+
version: '0.10'
|
|
20
19
|
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '0'
|
|
25
|
+
version: '0.10'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: rubyzip
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
30
|
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '3'
|
|
34
33
|
type: :runtime
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
37
|
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
41
|
-
description:
|
|
39
|
+
version: '3'
|
|
42
40
|
email: andrew@ankane.org
|
|
43
41
|
executables: []
|
|
44
42
|
extensions: []
|
|
@@ -54,7 +52,6 @@ homepage: https://github.com/ankane/npy
|
|
|
54
52
|
licenses:
|
|
55
53
|
- MIT
|
|
56
54
|
metadata: {}
|
|
57
|
-
post_install_message:
|
|
58
55
|
rdoc_options: []
|
|
59
56
|
require_paths:
|
|
60
57
|
- lib
|
|
@@ -62,15 +59,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
62
59
|
requirements:
|
|
63
60
|
- - ">="
|
|
64
61
|
- !ruby/object:Gem::Version
|
|
65
|
-
version: '3.
|
|
62
|
+
version: '3.3'
|
|
66
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
64
|
requirements:
|
|
68
65
|
- - ">="
|
|
69
66
|
- !ruby/object:Gem::Version
|
|
70
67
|
version: '0'
|
|
71
68
|
requirements: []
|
|
72
|
-
rubygems_version:
|
|
73
|
-
signing_key:
|
|
69
|
+
rubygems_version: 4.0.6
|
|
74
70
|
specification_version: 4
|
|
75
71
|
summary: Save and load NumPy npy and npz files in Ruby
|
|
76
72
|
test_files: []
|