Documentation
¶
Overview ¶
Package npz provides read/write access to files with compressed NumPy data file format:
https://numpy.org/neps/nep-0001-npy-format.html
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Read ¶
Read reads the item named name from the reader r and stores the extracted data into ptr.
Example ¶
package main
import (
"fmt"
"log"
"os"
"codeberg.org/sbinet/npyio/npz"
)
func main() {
f, err := os.Open("../testdata/data_float64_corder.npz")
if err != nil {
log.Fatalf("could not open npz file: %+v", err)
}
defer f.Close()
var f0 []float64
err = npz.Read(f, "arr0.npy", &f0)
if err != nil {
log.Fatalf("could not read value from npz file: %+v", err)
}
var f1 []float64
err = npz.Read(f, "arr1.npy", &f1)
if err != nil {
log.Fatalf("could not read value from npz file: %+v", err)
}
fmt.Printf("arr0: %v\n", f0)
fmt.Printf("arr1: %v\n", f1)
}
Output: arr0: [0 1 2 3 4 5] arr1: [0 1 2 3 4 5]
func Write ¶
Write writes the values vs to the named npz archive file.
The data-array will always be written out in C-order (row-major).
Example ¶
package main
import (
"log"
"codeberg.org/sbinet/npyio/npz"
)
func main() {
err := npz.Write("out.npz", map[string]any{
"arr0.npy": []float64{0, 1, 2, 3, 4, 5},
"arr1.npy": []float32{0, 1, 2, 3, 4, 5},
})
if err != nil {
log.Fatalf("could not save to npz file: %+v", err)
}
}
Output:
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads data from a compressed NumPy data file.
Example ¶
package main
import (
"fmt"
"log"
"os"
"codeberg.org/sbinet/npyio/npz"
)
func main() {
f, err := os.Open("../testdata/data_float64_corder.npz")
if err != nil {
log.Fatalf("could not open npz file: %+v", err)
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
log.Fatalf("could not stat npz file: %+v", err)
}
r, err := npz.NewReader(f, stat.Size())
if err != nil {
log.Fatalf("could not open npz archive: %+v", err)
}
for _, name := range r.Keys() {
fmt.Printf("%s: %v\n", name, r.Header(name))
}
var f0 []float64
err = r.Read("arr0.npy", &f0)
if err != nil {
log.Fatalf("could not read value from npz file: %+v", err)
}
var f1 []float64
err = r.Read("arr1.npy", &f1)
if err != nil {
log.Fatalf("could not read value from npz file: %+v", err)
}
fmt.Printf("arr0: %v\n", f0)
fmt.Printf("arr1: %v\n", f1)
}
Output: arr1.npy: Header{Major:1, Minor:0, Descr:{Type:'<f8', Fortran:false, Shape:[6 1]}} arr0.npy: Header{Major:1, Minor:0, Descr:{Type:'<f8', Fortran:false, Shape:[2 3]}} arr0: [0 1 2 3 4 5] arr1: [0 1 2 3 4 5]
func NewReader ¶
NewReader reads the compressed NumPy data from r, which is assumed to have the given size in bytes.
func Open ¶
Open opens the named compressed NumPy data file for reading.
Example ¶
package main
import (
"fmt"
"log"
"codeberg.org/sbinet/npyio/npz"
)
func main() {
f, err := npz.Open("../testdata/data_float64_corder.npz")
if err != nil {
log.Fatalf("could not open npz file: %+v", err)
}
defer f.Close()
for _, name := range f.Keys() {
fmt.Printf("%s: %v\n", name, f.Header(name))
}
var f0 []float64
err = f.Read("arr0.npy", &f0)
if err != nil {
log.Fatalf("could not read value from npz file: %+v", err)
}
var f1 []float64
err = f.Read("arr1.npy", &f1)
if err != nil {
log.Fatalf("could not read value from npz file: %+v", err)
}
fmt.Printf("arr0: %v\n", f0)
fmt.Printf("arr1: %v\n", f1)
}
Output: arr1.npy: Header{Major:1, Minor:0, Descr:{Type:'<f8', Fortran:false, Shape:[6 1]}} arr0.npy: Header{Major:1, Minor:0, Descr:{Type:'<f8', Fortran:false, Shape:[2 3]}} arr0: [0 1 2 3 4 5] arr1: [0 1 2 3 4 5]
func (*Reader) Close ¶
Close closes the NumPy compressed data reader. Close doesn't close the underlying reader.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes data to a compressed NumPy data file.
Example ¶
package main
import (
"log"
"os"
"codeberg.org/sbinet/npyio/npz"
)
func main() {
f, err := os.Create("out.npz")
if err != nil {
log.Fatalf("could not create npz file: %+v", err)
}
defer f.Close()
wz := npz.NewWriter(f)
defer wz.Close()
err = wz.Write("arr0.npy", []float64{0, 1, 2, 3, 4, 5})
if err != nil {
log.Fatalf("could not write value arr0.npy to npz file: %+v", err)
}
err = wz.Write("arr1.npy", []float32{0, 1, 2, 3, 4, 5})
if err != nil {
log.Fatalf("could not write value arr1.npy to npz file: %+v", err)
}
err = wz.Close()
if err != nil {
log.Fatalf("could not close npz archive: %+v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("could not close npz file: %+v", err)
}
}
Output:
func Create ¶
Create creates the named compressed NumPy data file for writing.
Example ¶
package main
import (
"log"
"codeberg.org/sbinet/npyio/npz"
)
func main() {
f, err := npz.Create("out.npz")
if err != nil {
log.Fatalf("could not create npz file: %+v", err)
}
defer f.Close()
err = f.Write("arr0.npy", []float64{0, 1, 2, 3, 4, 5})
if err != nil {
log.Fatalf("could not write value arr0.npy to npz file: %+v", err)
}
err = f.Write("arr1.npy", []float32{0, 1, 2, 3, 4, 5})
if err != nil {
log.Fatalf("could not write value arr1.npy to npz file: %+v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("could not close npz file: %+v", err)
}
}
Output:
func NewWriter ¶
NewWriter returns a new npz writer.
The returned npz writer won't close the underlying writer.