-
Notifications
You must be signed in to change notification settings - Fork 8
/
npyio_example_test.go
147 lines (121 loc) · 3.13 KB
/
npyio_example_test.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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.
package npyio_test
import (
"bytes"
"fmt"
"log"
"os"
"gonum.org/v1/gonum/mat"
"github.com/sbinet/npyio"
)
func ExampleWrite() {
m := mat.NewDense(2, 3, []float64{0, 1, 2, 3, 4, 5})
fmt.Printf("-- original data --\n")
fmt.Printf("data = %v\n", mat.Formatted(m, mat.Prefix(" ")))
buf := new(bytes.Buffer)
err := npyio.Write(buf, m)
if err != nil {
log.Fatalf("error writing data: %v\n", err)
}
// modify original data
m.Set(0, 0, 6)
var data mat.Dense
err = npyio.Read(buf, &data)
if err != nil {
log.Fatalf("error reading data: %v\n", err)
}
fmt.Printf("-- data read back --\n")
fmt.Printf("data = %v\n", mat.Formatted(&data, mat.Prefix(" ")))
fmt.Printf("-- modified original data --\n")
fmt.Printf("data = %v\n", mat.Formatted(m, mat.Prefix(" ")))
// Output:
// -- original data --
// data = ⎡0 1 2⎤
// ⎣3 4 5⎦
// -- data read back --
// data = ⎡0 1 2⎤
// ⎣3 4 5⎦
// -- modified original data --
// data = ⎡6 1 2⎤
// ⎣3 4 5⎦
}
func ExampleRead() {
m := mat.NewDense(2, 3, []float64{0, 1, 2, 3, 4, 5})
fmt.Printf("-- original data --\n")
fmt.Printf("data = %v\n", mat.Formatted(m, mat.Prefix(" ")))
buf := new(bytes.Buffer)
err := npyio.Write(buf, m)
if err != nil {
log.Fatalf("error writing data: %v\n", err)
}
// modify original data
m.Set(0, 0, 6)
var data mat.Dense
err = npyio.Read(buf, &data)
if err != nil {
log.Fatalf("error reading data: %v\n", err)
}
fmt.Printf("-- data read back --\n")
fmt.Printf("data = %v\n", mat.Formatted(&data, mat.Prefix(" ")))
fmt.Printf("-- modified original data --\n")
fmt.Printf("data = %v\n", mat.Formatted(m, mat.Prefix(" ")))
// Output:
// -- original data --
// data = ⎡0 1 2⎤
// ⎣3 4 5⎦
// -- data read back --
// data = ⎡0 1 2⎤
// ⎣3 4 5⎦
// -- modified original data --
// data = ⎡6 1 2⎤
// ⎣3 4 5⎦
}
func Example_partialRead() {
out, err := os.Create("data.npy")
if err != nil {
log.Fatal(err)
}
defer out.Close()
f := []float64{0, 1, 2, 3, 4, 5}
fmt.Printf("-- original data --\n")
fmt.Printf("data = %v\n", f)
err = npyio.Write(out, f)
if err != nil {
log.Fatalf("error writing data: %v\n", err)
}
err = out.Close()
if err != nil {
log.Fatal(err)
}
in, err := os.Open("data.npy")
if err != nil {
log.Fatal(err)
}
defer in.Close()
r, err := npyio.NewReader(in)
if err != nil {
log.Fatal(err)
}
data := make([]float64, 3)
err = r.Read(&data)
if err != nil {
log.Fatalf("error reading data: %v\n", err)
}
fmt.Printf("-- partial data read back --\n")
fmt.Printf("data = %v\n", data)
err = r.Read(&data)
if err != nil {
log.Fatalf("error reading data: %v\n", err)
}
fmt.Printf("-- rest of data read back --\n")
fmt.Printf("data = %v\n", data)
// Output:
// -- original data --
// data = [0 1 2 3 4 5]
// -- partial data read back --
// data = [0 1 2]
// -- rest of data read back --
// data = [3 4 5]
}