npy 0.1.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 +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/lib/npy.rb +111 -0
- data/lib/npy/file.rb +26 -0
- data/lib/npy/version.rb +3 -0
- metadata +118 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d8382d17139cd8c1b47882130b5612fd7d748923ead1e763a46e9f0c1e819e08
|
|
4
|
+
data.tar.gz: 26355f76370731fb6aa3eead0291cb845afa532bdc5ce7bf35a42eccc52a3819
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5f3e1672b1a9f378bcddd7e8783880d062dd5ffa672b72026ef5a28d862076f2200f2f86fc447feb2bea5e0113606f7bb44b8bb2c174fb1a639d634cc3e745d1
|
|
7
|
+
data.tar.gz: a4562cfb5e8511d2624e687b4fa06a304ab07075f6eb44083decc00895618b044190467d06f0793fb44c826c7b0e2f2ae5b1c61216a87c57c62f4fca05d2704c
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2019 Andrew Kane
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Npy
|
|
2
|
+
|
|
3
|
+
Load NumPy `npy` and `npz` files in Ruby - no Python required
|
|
4
|
+
|
|
5
|
+
:fire: Uses [Numo::NArray](https://github.com/ruby-numo/numo-narray) for blazing performance
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application’s Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'npy'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Getting Started
|
|
16
|
+
|
|
17
|
+
### npy
|
|
18
|
+
|
|
19
|
+
`npy` files contain a single array
|
|
20
|
+
|
|
21
|
+
Load an `npy` file
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
arr = Npy.load("x.npy")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Load an `npy` string
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
byte_str = File.binread("x.npy")
|
|
31
|
+
arr = Npy.load_string(byte_str)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### npz
|
|
35
|
+
|
|
36
|
+
`npz` files contain multiple arrays
|
|
37
|
+
|
|
38
|
+
Load an `npz` file
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
arrs = Npy.load_npz("mnist.npz")
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Get keys
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
arrs.keys
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Get an array
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
arrs["x"]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Arrays are lazy loaded for performance
|
|
57
|
+
|
|
58
|
+
## Resources
|
|
59
|
+
|
|
60
|
+
- [npy format](https://docs.scipy.org/doc/numpy/reference/generated/numpy.lib.format.html#module-numpy.lib.format)
|
|
61
|
+
|
|
62
|
+
## History
|
|
63
|
+
|
|
64
|
+
View the [changelog](https://github.com/ankane/npy/blob/master/CHANGELOG.md)
|
|
65
|
+
|
|
66
|
+
## Contributing
|
|
67
|
+
|
|
68
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
|
69
|
+
|
|
70
|
+
- [Report bugs](https://github.com/ankane/npy/issues)
|
|
71
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/npy/pulls)
|
|
72
|
+
- Write, clarify, or fix documentation
|
|
73
|
+
- Suggest or add new features
|
data/lib/npy.rb
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# dependencies
|
|
2
|
+
require "numo/narray"
|
|
3
|
+
require "zip"
|
|
4
|
+
|
|
5
|
+
# modules
|
|
6
|
+
require "npy/file"
|
|
7
|
+
require "npy/version"
|
|
8
|
+
|
|
9
|
+
module Npy
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
|
|
12
|
+
MAGIC_STR = "\x93NUMPY".b
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def load(path)
|
|
16
|
+
with_file(path) do |f|
|
|
17
|
+
load_io(f)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def load_npz(path)
|
|
22
|
+
with_file(path) do |f|
|
|
23
|
+
load_npz_io(f)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def load_string(byte_str)
|
|
28
|
+
load_io(StringIO.new(byte_str))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# rubyzip not playing nicely with StringIO
|
|
32
|
+
# def load_npz_string(byte_str)
|
|
33
|
+
# load_npz_io(StringIO.new(byte_str))
|
|
34
|
+
# end
|
|
35
|
+
|
|
36
|
+
def load_io(io)
|
|
37
|
+
magic = io.read(6)
|
|
38
|
+
raise Error, "Invalid npy format" unless magic&.b == MAGIC_STR
|
|
39
|
+
|
|
40
|
+
major_version = io.read(1)
|
|
41
|
+
minor_version = io.read(1)
|
|
42
|
+
raise Error, "Unsupported version" unless major_version == "\x01".b
|
|
43
|
+
|
|
44
|
+
header_len = io.read(2).unpack1("S<")
|
|
45
|
+
header = io.read(header_len)
|
|
46
|
+
descr, fortran_order, shape = parse_header(header)
|
|
47
|
+
raise Error, "Fortran order not supported" if fortran_order
|
|
48
|
+
|
|
49
|
+
klass =
|
|
50
|
+
case descr
|
|
51
|
+
when "|i1"
|
|
52
|
+
Numo::Int8
|
|
53
|
+
when "<i2"
|
|
54
|
+
Numo::Int16
|
|
55
|
+
when "<i4"
|
|
56
|
+
Numo::Int32
|
|
57
|
+
when "<i8"
|
|
58
|
+
Numo::Int64
|
|
59
|
+
when "|u1"
|
|
60
|
+
Numo::UInt8
|
|
61
|
+
when "<u2"
|
|
62
|
+
Numo::UInt16
|
|
63
|
+
when "<u4"
|
|
64
|
+
Numo::UInt32
|
|
65
|
+
when "<u8"
|
|
66
|
+
Numo::UInt64
|
|
67
|
+
when "<f4"
|
|
68
|
+
Numo::SFloat
|
|
69
|
+
when "<f8"
|
|
70
|
+
Numo::DFloat
|
|
71
|
+
when "<c8"
|
|
72
|
+
Numo::SComplex
|
|
73
|
+
when "<c16"
|
|
74
|
+
Numo::DComplex
|
|
75
|
+
else
|
|
76
|
+
raise Error, "Type not supported: #{descr}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
klass.from_binary(io.read, shape)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def load_npz_io(io)
|
|
83
|
+
File.new(io)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def with_file(path)
|
|
89
|
+
::File.open(path, "rb") do |f|
|
|
90
|
+
yield f
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# header is Python dict, so use regex to parse
|
|
95
|
+
def parse_header(header)
|
|
96
|
+
# descr
|
|
97
|
+
m = /'descr': '([^']+)'/.match(header)
|
|
98
|
+
descr = m[1]
|
|
99
|
+
|
|
100
|
+
# fortran_order
|
|
101
|
+
m = /'fortran_order': ([^,]+)/.match(header)
|
|
102
|
+
fortran_order = m[1] == "True"
|
|
103
|
+
|
|
104
|
+
# shape
|
|
105
|
+
m = /'shape': \(([^)]+)\)/.match(header)
|
|
106
|
+
shape = m[1].split(", ").map(&:to_i)
|
|
107
|
+
|
|
108
|
+
[descr, fortran_order, shape]
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
data/lib/npy/file.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Npy
|
|
2
|
+
class File
|
|
3
|
+
def initialize(io)
|
|
4
|
+
@streams = {}
|
|
5
|
+
Zip::File.open_buffer(io) do |zipfile|
|
|
6
|
+
zipfile.each do |entry|
|
|
7
|
+
name = entry.name.sub(/\.npy\z/, "")
|
|
8
|
+
@streams[name] = entry.get_input_stream
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
@data = {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def keys
|
|
15
|
+
@streams.keys
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def [](name)
|
|
19
|
+
@data[name] ||= Npy.load_io(@streams[name]) if @streams[name]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_h
|
|
23
|
+
keys.map { |k| [k, self[k]] }.to_h
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/lib/npy/version.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: npy
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrew Kane
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-09-13 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: numo-narray
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rubyzip
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: minitest
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '5'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '5'
|
|
83
|
+
description:
|
|
84
|
+
email: andrew@chartkick.com
|
|
85
|
+
executables: []
|
|
86
|
+
extensions: []
|
|
87
|
+
extra_rdoc_files: []
|
|
88
|
+
files:
|
|
89
|
+
- CHANGELOG.md
|
|
90
|
+
- LICENSE.txt
|
|
91
|
+
- README.md
|
|
92
|
+
- lib/npy.rb
|
|
93
|
+
- lib/npy/file.rb
|
|
94
|
+
- lib/npy/version.rb
|
|
95
|
+
homepage: https://github.com/ankane/npy
|
|
96
|
+
licenses:
|
|
97
|
+
- MIT
|
|
98
|
+
metadata: {}
|
|
99
|
+
post_install_message:
|
|
100
|
+
rdoc_options: []
|
|
101
|
+
require_paths:
|
|
102
|
+
- lib
|
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '2.4'
|
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: '0'
|
|
113
|
+
requirements: []
|
|
114
|
+
rubygems_version: 3.0.3
|
|
115
|
+
signing_key:
|
|
116
|
+
specification_version: 4
|
|
117
|
+
summary: Load NumPy npy and npz files in Ruby
|
|
118
|
+
test_files: []
|