gonpy

package module
v0.0.0-...-fa3c8dd Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: BSD-3-Clause Imports: 7 Imported by: 8

README

Build Status Go Report Card codecov GoDoc

gonpy : Read Numpy binary files (.npy files) in Golang

gonpy reads and writes Numpy binary array data to/from Go slices

The npy file specification is here:

https://www.numpy.org/devdocs/reference/generated/numpy.lib.format.html

The documentation for this package can be found here:

https://godoc.org/github.com/kshedden/gonpy

When reading a multidimensional array, the data are returned as a one-dimensional slice that has been flattened as specified by the ColumnMajor field in the NpyReader struct. If ColumnMajor is true, the array is flattened in column major order. If ColumnMajor is false, the array is flattened in row major order.

The writer defaults to writing the data as a vector. The Shape field of the NpyWriter struct can be used to set other shapes.

Unsigned numeric data types, fixed-width string types, compound dtypes, record arrays, and time dtypes are not supported (file an issue if any of these are needed). Python object types are of course not supported.

The following example shows how to read a npy file into a slice of float64 values (for clarity error handling is omitted).

r, _ := gonpy.NewFileReader("data.npy")
data, _ := r.GetFloat64()

The reader value has fields such sas r.Shape that provide additional information about the array.

The following example shows how to write a slice of int32 values to a npy file (again, error handling is omitted).

w, _ := gonpy.NewFileWriter("data.npy")
_ = w.WriteFloat64(data)

To specify a shape or other attributes, modify the writer object before writing the array, for example:

w, _ := gonpy.NewFileWriter("data.npy")
w.Shape = []int32{50, 2}
w.Version = 2
_ = w.WriteFloat64(data)

To write to a stream, say a gzip stream, use the following:

f, _ := os.Create("data.npy.gz")
g := gzip.NewWriter(f)
w, _ := gonpy.NewWriter(g)
_ = w.WriteFloat64(data)
f.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NpyReader

type NpyReader struct {

	// The numpy data type of the array
	Dtype string

	// The endianness of the binary data
	Endian binary.ByteOrder

	// The version number of the file format
	Version int

	// The shape of the array as specified in the file.
	Shape []int

	// If true, the data are flattened in column-major order,
	// otherwise they are flattened in row-major order.
	ColumnMajor bool
	// contains filtered or unexported fields
}

NpyReader can read data from a Numpy binary array into a Go slice.

func NewFileReader

func NewFileReader(f string) (*NpyReader, error)

NewFileReader returns a NpyReader that can be used to obtain array data from the given named file. Call one of the GetXXX methods to obtain the data as a Go slice.

func NewReader

func NewReader(r io.Reader) (*NpyReader, error)

NewReader returns a NpyReader that can be used to obtain array data as a Go slice. The Go slice has a type matching the dtype in the Numpy file. Call one of the GetXX methods to obtain the slice.

func (*NpyReader) GetComplex128

func (rdr *NpyReader) GetComplex128() ([]complex128, error)

GetComplex128 returns the array data as a slice of complex128 values.

func (*NpyReader) GetComplex64

func (rdr *NpyReader) GetComplex64() ([]complex64, error)

GetComplex64 returns the array data as a slice of complex64 values.

func (*NpyReader) GetFloat32

func (rdr *NpyReader) GetFloat32() ([]float32, error)

GetFloat32 returns the array data as a slice of float32 values.

func (*NpyReader) GetFloat64

func (rdr *NpyReader) GetFloat64() ([]float64, error)

GetFloat64 returns the array data as a slice of float64 values.

func (*NpyReader) GetInt16

func (rdr *NpyReader) GetInt16() ([]int16, error)

GetInt16 returns the array data as a slice of int16 values.

func (*NpyReader) GetInt32

func (rdr *NpyReader) GetInt32() ([]int32, error)

GetInt32 returns the array data as a slice of int32 values.

func (*NpyReader) GetInt64

func (rdr *NpyReader) GetInt64() ([]int64, error)

GetInt64 returns the array data as a slice of int64 values.

func (*NpyReader) GetInt8

func (rdr *NpyReader) GetInt8() ([]int8, error)

GetInt8 returns the array data as a slice of int8 values.

func (*NpyReader) GetUint16

func (rdr *NpyReader) GetUint16() ([]uint16, error)

GetUint16 returns the array data as a slice of uint16 values.

func (*NpyReader) GetUint32

func (rdr *NpyReader) GetUint32() ([]uint32, error)

GetUint32 returns the array data as a slice of uint32 values.

func (*NpyReader) GetUint64

func (rdr *NpyReader) GetUint64() ([]uint64, error)

GetUint64 returns the array data as a slice of uint64 values.

func (*NpyReader) GetUint8

func (rdr *NpyReader) GetUint8() ([]uint8, error)

GetUint8 returns the array data as a slice of uint8 values.

type NpyWriter

type NpyWriter struct {

	// Defaults to little endian, but can be set to
	// binary.BigEndian before writing data
	Endian binary.ByteOrder

	// Defaults to nx1, where n is the length of data.  Can be set
	// to any shape with any number of dimensions.  The shape is
	// not checked for compatibility with the data.
	Shape []int

	// Defaults to false (row major order), can be set to true
	// (column major order) before writing the data.
	ColumnMajor bool

	// Defaults to 1, can be set to 2 before writing the data.
	Version int
	// contains filtered or unexported fields
}

NpyWriter can write data from a Go slice to a Numpy binary array.

func NewFileWriter

func NewFileWriter(fname string) (*NpyWriter, error)

NewFileWriter returns a NpyWriter that can be used to write data to a Numpy binary format file. After creation, call one of the WriteXX methods to write array data to the file. The file is automatically closed at the end of that call. Only one array can be written to a file.

func NewWriter

func NewWriter(w io.WriteCloser) (*NpyWriter, error)

NewWriter returns a NpyWriter that can be used to write data to an io.WriteCloser, using the Numpy binary format. After creation, call one of the WriteXXX methods to write array data to the writer. The file is automatically closed at the end of that call. Only one slice can be written to the writer.

func (*NpyWriter) WriteComplex128

func (wtr *NpyWriter) WriteComplex128(data []complex128) error

WriteComplex128 writes a slice of complex128 values in npy format.

func (*NpyWriter) WriteComplex64

func (wtr *NpyWriter) WriteComplex64(data []complex64) error

WriteComplex64 writes a slice of complex64 values in npy format.

func (*NpyWriter) WriteFloat32

func (wtr *NpyWriter) WriteFloat32(data []float32) error

WriteFloat32 writes a slice of float32 values in npy format.

func (*NpyWriter) WriteFloat64

func (wtr *NpyWriter) WriteFloat64(data []float64) error

WriteFloat64 writes a slice of float64 values in npy format.

func (*NpyWriter) WriteInt16

func (wtr *NpyWriter) WriteInt16(data []int16) error

WriteInt16 writes a slice of int16 values in npy format.

func (*NpyWriter) WriteInt32

func (wtr *NpyWriter) WriteInt32(data []int32) error

WriteInt32 writes a slice of int32 values in npy format.

func (*NpyWriter) WriteInt64

func (wtr *NpyWriter) WriteInt64(data []int64) error

WriteInt64 writes a slice of int64 values in npy format.

func (*NpyWriter) WriteInt8

func (wtr *NpyWriter) WriteInt8(data []int8) error

WriteInt8 writes a slice of int8 values in npy format.

func (*NpyWriter) WriteUint16

func (wtr *NpyWriter) WriteUint16(data []uint16) error

WriteUint16 writes a slice of uint16 values in npy format.

func (*NpyWriter) WriteUint32

func (wtr *NpyWriter) WriteUint32(data []uint32) error

WriteUint32 writes a slice of uint32 values in npy format.

func (*NpyWriter) WriteUint64

func (wtr *NpyWriter) WriteUint64(data []uint64) error

WriteUint64 writes a slice of uint64 values in npy format.

func (*NpyWriter) WriteUint8

func (wtr *NpyWriter) WriteUint8(data []uint8) error

WriteUint8 writes a slice of uint8 values in npy format.

Jump to

Keyboard shortcuts

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