Skip to content

Commit

Permalink
Added mixin override
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Apr 25, 2016
1 parent 5897772 commit ff1020a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
22 changes: 22 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ func getUserLocations(c *gin.Context) {
}
}

func putMixinOverride(c *gin.Context) {
group := strings.ToLower(c.DefaultQuery("group", "noneasdf"))
newMixin := c.DefaultQuery("mixin", "none")
if group != "noneasdf" {
fmt.Println(group, newMixin)
newMixinFloat, err := strconv.ParseFloat(newMixin, 64)
if err == nil {
err2 := setMixinOverride(group, newMixinFloat)
if err2 == nil {
mixinOverrideCache[group] = newMixinFloat
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Overriding mixin for " + group + ", now set to " + newMixin})
} else {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err2.Error()})
}
} else {
c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
}
} else {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Error parsing request"})
}
}

func editNetworkName(c *gin.Context) {
group := c.DefaultQuery("group", "noneasdf")
oldname := c.DefaultQuery("oldname", "none")
Expand Down
2 changes: 2 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var psCache map[string]FullParameters
var usersCache map[string][]string
var userPositionCache map[string]UserPositionJSON
var isLearning map[string]bool
var mixinOverrideCache map[string]float64

func init() {
go clearCache()
Expand All @@ -17,6 +18,7 @@ func clearCache() {
psCache = make(map[string]FullParameters)
usersCache = make(map[string][]string)
userPositionCache = make(map[string]UserPositionJSON)
mixinOverrideCache = make(map[string]float64)
time.Sleep(time.Minute * 10)
}
}
8 changes: 7 additions & 1 deletion parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"path"
"strconv"
"strings"

"github.com/boltdb/bolt"
)
Expand Down Expand Up @@ -304,6 +305,10 @@ func getParameters(group string, ps *FullParameters, fingerprintsInMemory map[st
}

func getMixinOverride(group string) (float64, error) {
group = strings.ToLower(group)
if val, ok := mixinOverrideCache[group]; ok {
return val, nil
}
override := float64(-1)
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
defer db.Close()
Expand All @@ -324,11 +329,12 @@ func getMixinOverride(group string) (float64, error) {
override, err = strconv.ParseFloat(string(v), 64)
return err
})
mixinOverrideCache[group] = override
return override, err
}

func setMixinOverride(group string, mixin float64) error {
if mixin < 0 || mixin > 1 {
if (mixin < 0 || mixin > 1) && mixin != -1 {
return fmt.Errorf("mixin must be between 0 and 1")
}
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0600, nil)
Expand Down
7 changes: 6 additions & 1 deletion posterior.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ func calculatePosterior(res Fingerprint, ps FullParameters) (string, map[string]
PBayesMix := make(map[string]float64)
bestLocation := ""
maxVal := float64(-100)
mixin := ps.Priors[n].Special["MixIn"]
mixinOverride, _ := getMixinOverride(res.Group)
if mixinOverride >= 0 || mixinOverride <= 1 {
mixin = mixinOverride
}
for key := range PBayes1 {
PBayesMix[key] = ps.Priors[n].Special["MixIn"]*PBayes1[key] + (1-ps.Priors[n].Special["MixIn"])*PBayes2[key]
PBayesMix[key] = mixin*PBayes1[key] + (1-mixin)*PBayes2[key]
if PBayesMix[key] > maxVal {
maxVal = PBayesMix[key]
bestLocation = key
Expand Down
7 changes: 6 additions & 1 deletion priorsThreaded.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,13 @@ func optimizePriorsThreaded(group string) {
}

// Load new priors and calculate new cross Validation
mixinOverride, _ := getMixinOverride(group)
for n := range ps.Priors {
ps.Priors[n].Special["MixIn"] = bestMixin[n]
if mixinOverride != -1 {
ps.Priors[n].Special["MixIn"] = mixinOverride
} else {
ps.Priors[n].Special["MixIn"] = bestMixin[n]
}
ps.Priors[n].Special["VarabilityCutoff"] = bestCutoff[n]
crossValidation(group, n, &ps, fingerprintsInMemory, fingerprintsOrdering)
}
Expand Down
7 changes: 6 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ func slashDashboard(c *gin.Context) {
dash.LocationCount = make(map[string]int)
dash.Mixin = make(map[string]float64)
dash.VarabilityCutoff = make(map[string]float64)
mixinOverride, _ := getMixinOverride(group)
for n := range ps.NetworkLocs {
dash.Mixin[n] = ps.Priors[n].Special["MixIn"]
if mixinOverride != -1 {
dash.Mixin[n] = mixinOverride
} else {
dash.Mixin[n] = ps.Priors[n].Special["MixIn"]
}
dash.VarabilityCutoff[n] = ps.Priors[n].Special["VarabilityCutoff"]
dash.Networks = append(dash.Networks, n)
dash.Locations[n] = []string{}
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Options:`)
r.GET("/track", trackFingerprint)

// API routes (api.go)
r.PUT("/mixin", putMixinOverride)
r.GET("/location", getUserLocations)
r.GET("/whereami", whereAmI)
r.GET("/editname", editName)
Expand Down

0 comments on commit ff1020a

Please sign in to comment.