matf

package module
v0.0.0-...-937d4e2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2018 License: MIT Imports: 11 Imported by: 0

README

matf Build Status Coverage Status Go Report Card GoDoc

This is matf and it is written in golang. matf extracts the content from MATLABs MAT-files.

Why?

The idea of this project is, to make the content of mat-files available in golang.

Example

Load a simple matrix, saved in a matf-file, in gonum/mat.

package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"reflect"

	"github.com/florianl/matf"
	"gonum.org/v1/gonum/mat"
)

func main() {

	modelfile, err := matf.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}
	defer matf.Close(modelfile)

	element, err := matf.ReadDataElement(modelfile)
	if err != nil && err != io.EOF {
		log.Fatal(err)
		return
	}
	r, c, _, err := element.Dimensions()
	data := []float64{}
	slice := reflect.ValueOf(element.Content.(matf.NumPrt).RealPart)
	for i := 0; i < slice.Len(); i++ {
		value := reflect.ValueOf(slice.Index(i).Interface()).Float()
		data = append(data, value)
	}

	dense := mat.NewDense(r, c, data)
	fmt.Printf("dense = %v\n", mat.Formatted(dense, mat.Prefix("        ")))

}

Simple example, using gorgonia.

package main

import (
	"io"
	"log"
	"os"
	"reflect"

	"github.com/florianl/matf"
	"gorgonia.org/gorgonia"
	"gorgonia.org/tensor"

)

func main() {

	modelfile, err := matf.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}
	defer matf.Close(modelfile)

	element, err := matf.ReadDataElement(modelfile)
	if err != nil && err != io.EOF {
		log.Fatal(err)
		return
	}
	r, c, _, err := element.Dimensions()
	data := []float64{}
	slice := reflect.ValueOf(element.Content.(matf.NumPrt).RealPart)
	for i := 0; i < slice.Len(); i++ {
		value := reflect.ValueOf(slice.Index(i).Interface()).Float()
		data = append(data, value)
	}

	t := tensor.New(tensor.WithShape(r,c), tensor.WithBacking(data))

	g := gorgonia.NewGraph()
	w := gorgonia.NewMatrix(g, gorgonia.Float64, gorgonia.WithShape(r,c), gorgonia.WithValue(t))
	gorgonia.Must(gorgonia.Sigmoid(w))

	m := gorgonia.NewTapeMachine(g)
	if err := m.RunAll(); err != nil {
		log.Fatal(err)
		return
	}

	m.Reset()
}

Documentation

Overview

Package matf extracts the content from MAT-files and make it available in golang. In golang, then you can use your favorite Machine Learning environment, to further use of the extracted data.

For example, you can use the data in gonum:

package main
import (
	"fmt"
	"io"
	"log"
	"os"
	"reflect"

	"github.com/florianl/matf"
	"gonum.org/v1/gonum/mat"
)
func main() {
	modelfile, err := matf.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}
	defer matf.Close(modelfile)
	element, err := matf.ReadDataElement(modelfile)
	if err != nil && err != io.EOF {
		log.Fatal(err)
		return
	}
	r, c, _, err := element.Dimensions()
	data := []float64{}
	slice := reflect.ValueOf(element.Content.(matf.NumPrt).RealPart)
	for i := 0; i < slice.Len(); i++ {
		value := reflect.ValueOf(slice.Index(i).Interface()).Float()
		data = append(data, value)
	}
	dense := mat.NewDense(r, c, data)
	fmt.Printf("dense = %v\n", mat.Formatted(dense, mat.Prefix("        ")))
}

Or use it in gorgonia:

package main
import (
	"io"
	"log"
	"os"
	"reflect"

	"github.com/florianl/matf"
	"gorgonia.org/gorgonia"
	"gorgonia.org/tensor"
)
func main() {
	modelfile, err := matf.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}
	defer matf.Close(modelfile)
	element, err := matf.ReadDataElement(modelfile)
	if err != nil && err != io.EOF {
		log.Fatal(err)
		return
	}
	r, c, _, err := element.Dimensions()
	data := []float64{}
	slice := reflect.ValueOf(element.Content.(matf.NumPrt).RealPart)
	for i := 0; i < slice.Len(); i++ {
		value := reflect.ValueOf(slice.Index(i).Interface()).Float()
		data = append(data, value)
	}
	t := tensor.New(tensor.WithShape(r,c), tensor.WithBacking(data))
	g := gorgonia.NewGraph()
	w := gorgonia.NewMatrix(g, gorgonia.Float64, gorgonia.WithShape(r,c), gorgonia.WithValue(t))
	gorgonia.Must(gorgonia.Sigmoid(w))
	m := gorgonia.NewTapeMachine(g)
	if err := m.RunAll(); err != nil {
		log.Fatal(err)
		return
	}
	m.Reset()
}

Index

Constants

View Source
const (
	MiInt8       int = 1
	MiUint8      int = 2
	MiInt16      int = 3
	MiUint16     int = 4
	MiInt32      int = 5
	MiUint32     int = 6
	MiSingle     int = 7
	MiDouble     int = 9
	MiInt64      int = 12
	MiUint64     int = 13
	MiMatrix     int = 14
	MiCompressed int = 15
	MiUtf8       int = 16
	MiUtf16      int = 17
	MiUtf32      int = 18
)

List of all MAT-File Data Types

View Source
const (
	MxCellClass   int = 1
	MxStructClass int = 2
	MxObjectClass int = 3
	MxCharClass   int = 4
	MxSparseClass int = 5
	MxDoubleClass int = 6
	MxSingleClass int = 7
	MxInt8Class   int = 8
	MxUint8Class  int = 9
	MxInt16Class  int = 10
	MxUint16Class int = 11
	MxInt32Class  int = 12
	MxUint32Class int = 13
	MxInt64Class  int = 14
	MxUint64Class int = 15
)

List of all MAT-File Array Types

View Source
const (
	ClassMask   = 0x0F    // Mask to extract the containing class from an array.
	FlagComplex = 1 << 11 // If set, the data element contains an imaginary part.
	FlagGlobal  = 1 << 10 // MATLAB uses this element on global scope.
	FlagLogical = 1 << 9  // Array is used for logical indexing.
)

Basic binary flags for various array types

Variables

This section is empty.

Functions

func Close

func Close(file *Matf) error

Close a MAT-file

Types

type CellPrt

type CellPrt struct {
	Cells []MatMatrix
}

CellPrt represents a list of matf cells

type CharPrt

type CharPrt struct {
	Chars []string
}

CharPrt represents a matf char array

type Dim

type Dim struct {
	X, Y, Z int
}

Dim contains the dimensions of a MatMatrix

type Header struct {
	Text                string // Some kind of descriptive text containing various information.
	SubsystemDataOffset []byte // Contains the sybsystem-specific data.
	Version             uint16 // MATLAB version used, to create this file.
	EndianIndicator     uint16 // Indicates, if the file was written on a Big Endian or Little Endian system.
}

Header contains informations about the MAT-file

type MatMatrix

type MatMatrix struct {
	Name  string
	Flags uint32
	Class uint32
	Dim
	Content interface{} // Can contain NumPrt, StructPrt, CellPrt or CharPrt - depending on the value in Class.
}

MatMatrix represents a matrix

func ReadDataElement

func ReadDataElement(file *Matf) (MatMatrix, error)

ReadDataElement returns the next data element. It returns io.EOF, if no further elements are available

func (MatMatrix) Dimensions

func (m MatMatrix) Dimensions() (int, int, int, error)

Dimensions returns the dimensions of a matrix

type Matf

type Matf struct {
	Header
	// contains filtered or unexported fields
}

Matf represents the MAT-file

func Open

func Open(file string) (*Matf, error)

Open a MAT-file and extracts the header information into the Header struct.

type NumPrt

type NumPrt struct {
	RealPart      interface{}
	ImaginaryPart interface{}
}

NumPrt contains the numeric part of a matrix

type StructPrt

type StructPrt struct {
	FieldNames  []string
	FieldValues map[string][]interface{}
}

StructPrt represents a matf struct

Jump to

Keyboard shortcuts

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