-
Notifications
You must be signed in to change notification settings - Fork 25
Embed
See https://github.com/CompCogNeuro/sims for examples -- all of these have embedded "assets"
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 using the GOPATH method:
$ go get github.com/shuLhan/go-bindata
$ cd ~/go/src/github.com/shuLhan/go-bindata
$ cd cmd/go-bindata
$ go install # installs into ~/go/bin -- assuming that is on your PATH
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.
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")
}
// 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")
}
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.
test