Skip to content
Randall O'Reilly edited this page Feb 12, 2021 · 2 revisions

Embedding files to create a standalone executable

See https://github.com/CompCogNeuro/sims for examples -- all of these have embedded "assets"

go-bindata program

go-bindata is a program that takes a list of files and embeds them as []byte constants in a file called bindata.go, along with built-in functions in that same file that provide various standard Go ways of accessing that data as you would a file.

There are many forks of the original version of this program -- the one we use is:

Install:

$ go get github.com/shuLhan/go-bindata/...

Then you just run it in your simulation directory, with a list of files to encode:

$ go-bindata my-weights.wts my-pats.tsv

this creates the bindata.go file.

Reading the Asset files

etable.Table files (e.g., Patterns)

This is code for reading patterns to present to a network into an etable.Table:

// OpenPatAsset opens pattern file from embedded assets
func (ss *Sim) OpenPatAsset(dt *etable.Table, fnm, name, desc string) error {
	dt.SetMetaData("name", name)
	dt.SetMetaData("desc", desc)
	ab, err := Asset(fnm)  // this gives you the []byte directly
	if err != nil {
		log.Println(err)
		return err
	}
	err = dt.ReadCSV(bytes.NewBuffer(ab), etable.Tab)   // Read methods operate on an io.Reader
	if err != nil {
		log.Println(err)
	}
	return err
}

// OpenPats opens the files
func (ss *Sim) OpenPats() {
	ss.OpenPatAsset(ss.Lines2, "lines_5x5x2.tsv", "Lines2", "Lines2 Training patterns")
	ss.OpenPatAsset(ss.Lines1, "lines_5x5x1.tsv", "Lines1", "Lines1 Testing patterns")
}

Reading weight files

// OpenTrainedWts opens trained weights
func (ss *Sim) OpenTrainedWts() {
	ab, err := Asset("objrec_train1.wts") // embedded in executable
	if err != nil {
		log.Println(err)
	}
	ss.Net.ReadWtsJSON(bytes.NewBuffer(ab))
	// ss.Net.OpenWtsJSON("objrec_train1.wts.gz")
}

Makefile target

it is convenient to make a Makefile with a target that makes the bindata:

# important: update these for each app
ASSETS=lines_5x5x1.tsv lines_5x5x2.tsv

# must do: go get github.com/shuLhan/go-bindata -- go install in cmd/go-bindata
bindata:
	go-bindata $(ASSETS)

so you can just type make bindata to regenerate the bindata.go file if you need to update them.