npz

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

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

func Read(r io.ReaderAt, name string, ptr any) error

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

func Write(name string, vs map[string]any) error

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)
	}

}

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

func NewReader(r io.ReaderAt, size int64) (*Reader, error)

NewReader reads the compressed NumPy data from r, which is assumed to have the given size in bytes.

func Open

func Open(name string) (*Reader, error)

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

func (r *Reader) Close() error

Close closes the NumPy compressed data reader. Close doesn't close the underlying reader.

func (*Reader) Header

func (r *Reader) Header(name string) *npy.Header

Header returns the NumPy header metadata for the named array.

func (*Reader) Keys

func (r *Reader) Keys() []string

Keys returns the names of the NumPy data arrays.

func (*Reader) Open

func (r *Reader) Open(name string) (io.ReadCloser, error)

Open opens the named npy section in the npz archive.

func (*Reader) Read

func (r *Reader) Read(name string, ptr any) error

Read reads the named NumPy array data into the provided pointer.

Read returns an error if the on-disk data type and the provided one don't match.

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)
	}

}

func Create

func Create(name string) (*Writer, error)

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)
	}

}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new npz writer.

The returned npz writer won't close the underlying writer.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the npz archive. Close flushes the data to disk.

func (*Writer) Write

func (w *Writer) Write(name string, v any) error

Write writes the named NumPy array data to the npz archive.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL