Skip to content

Commit

Permalink
Merge pull request #30 from rhorber/patch-1
Browse files Browse the repository at this point in the history
Warn about invalid model name
  • Loading branch information
zhenghaoz committed Feb 6, 2020
2 parents b1416ec + 3a25a5e commit 467a005
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions engine/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/zhenghaoz/gorse/base"
"github.com/zhenghaoz/gorse/core"
"fmt"
"log"
)

Expand Down Expand Up @@ -84,6 +85,10 @@ func UpdateRecommends(name string, params base.Params, cacheSize int, fitJobs in
// Create model
log.Printf("create model %v with params = %v\n", name, params)
model := LoadModel(name, params)
if model == nil {
log.Printf("invalid model %v, aborting\n", name)
return fmt.Errorf("invalid model %v", name)
}
// Training model
log.Println("training model")
model.Fit(dataSet, &base.RuntimeOptions{Verbose: true, FitJobs: fitJobs})
Expand Down
29 changes: 29 additions & 0 deletions engine/offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,32 @@ func TestUpdateRecommends(t *testing.T) {
t.Fatal(err)
}
}

func TestUpdateRecommendsInvalidModel(t *testing.T) {
// Create database
fileName := path.Join(core.TempDir, randstr.String(16))
db, err := Open(fileName)
if err != nil {
t.Fatal(err)
}
// Update recommends
dataSet := core.LoadDataFromBuiltIn("ml-100k")
itemId := make([]string, dataSet.ItemCount())
for itemIndex := 0; itemIndex < dataSet.ItemCount(); itemIndex++ {
itemId[itemIndex] = dataSet.ItemIndexer().ToID(itemIndex)
}
if err = db.InsertItems(itemId, nil); err != nil {
t.Fatal(err)
}
if err = UpdateRecommends("invalid-model", nil, 10, runtime.NumCPU(), dataSet, db); err == nil {
t.Fatal("function should return an error")
}
// Close database
if err = db.Close(); err != nil {
t.Fatal(err)
}
// Clean database
if err = os.Remove(fileName); err != nil {
t.Fatal(err)
}
}

0 comments on commit 467a005

Please sign in to comment.