forked from sbinet/npyio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npyio.go
128 lines (114 loc) · 3.68 KB
/
npyio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2016 The npyio Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate embedmd -w README.md
// Package npyio provides read/write access to files following the NumPy data file format:
//
// https://numpy.org/neps/nep-0001-npy-format.html
//
// # Supported types
//
// npyio supports r/w of scalars, arrays, slices and gonum/mat.Dense.
// Supported scalars are:
// - bool,
// - (u)int{8,16,32,64},
// - float{32,64},
// - complex{64,128}
//
// # Reading
//
// Reading from a NumPy data file can be performed like so:
//
// f, err := os.Open("data.npy")
// var m mat.Dense
// err = npyio.Read(f, &m)
// fmt.Printf("data = %v\n", mat.Formatted(&m, mat.Prefix(" "))))
//
// npyio can also read data directly into slices, arrays or scalars, provided
// the on-disk data type and the provided one match.
//
// Example:
//
// var data []float64
// err = npyio.Read(f, &data)
//
// var data uint64
// err = npyio.Read(f, &data)
//
// # Writing
//
// Writing into a NumPy data file can be done like so:
//
// f, err := os.Create("data.npy")
// var m mat.Dense = ...
// err = npyio.Write(f, m)
//
// Scalars, arrays and slices are also supported:
//
// var data []float64 = ...
// err = npyio.Write(f, data)
//
// var data int64 = 42
// err = npyio.Write(f, data)
//
// var data [42]complex128 = ...
// err = npyio.Write(f, data)
package npyio
import (
"io"
"reflect"
"github.com/sbinet/npyio/npy"
)
var (
// ErrInvalidNumPyFormat is the error returned by NewReader when
// the underlying io.Reader is not a valid or recognized NumPy data
// file format.
ErrInvalidNumPyFormat = npy.ErrInvalidNumPyFormat
// ErrTypeMismatch is the error returned by Reader when the on-disk
// data type and the user provided one do NOT match.
ErrTypeMismatch = npy.ErrTypeMismatch
// ErrInvalidType is the error returned by Reader and Writer when
// confronted with a type that is not supported or can not be
// reliably (de)serialized.
ErrInvalidType = npy.ErrInvalidType
// Magic header present at the start of a NumPy data file format.
// See https://numpy.org/neps/nep-0001-npy-format.html
Magic = npy.Magic
)
// Header describes the data content of a NumPy data file.
type Header = npy.Header
// Reader reads data from a NumPy data file.
type Reader = npy.Reader
// NewReader creates a new NumPy data file format reader.
func NewReader(r io.Reader) (*Reader, error) {
return npy.NewReader(r)
}
// Read reads the data from the r NumPy data file io.Reader, into the
// provided pointed at value ptr.
// Read returns an error if the on-disk data type and the one provided
// don't match.
//
// If a *mat.Dense matrix is passed to Read, the numpy-array data is loaded
// into the Dense matrix, honouring Fortran/C-order and dimensions/shape
// parameters.
//
// Only numpy-arrays with up to 2 dimensions are supported.
// Only numpy-arrays with elements convertible to float64 are supported.
func Read(r io.Reader, ptr interface{}) error {
return npy.Read(r, ptr)
}
// TypeFrom returns the reflect.Type corresponding to the numpy-dtype string, if any.
func TypeFrom(dtype string) reflect.Type {
return npy.TypeFrom(dtype)
}
// Write writes 'val' into 'w' in the NumPy data format.
//
// - if val is a scalar, it must be of a supported type (bools, (u)ints, floats and complexes)
// - if val is a slice or array, it must be a slice/array of a supported type.
// the shape (len,) will be written out.
// - if val is a mat.Dense, the correct shape will be transmitted. (ie: (nrows, ncols))
//
// The data-array will always be written out in C-order (row-major).
func Write(w io.Writer, val interface{}) error {
return npy.Write(w, val)
}