Skip to content
Snippets Groups Projects
Commit e49c023a authored by Tucker Siegel's avatar Tucker Siegel
Browse files

change logic

parent 1f820123
No related branches found
No related tags found
No related merge requests found
package services
import (
"fmt"
"math"
"sort"
"time"
"github.com/montanaflynn/stats"
......@@ -94,8 +96,8 @@ func CalculateStages(ctx common.DawnCtx, request models.StageRequest) map[string
}
normal, _ := stats.Mean(accs2)
min, _ := stats.PercentileNearestRank(accs2, 5)
max, _ := stats.PercentileNearestRank(accs2, 95)
min, _ := stats.PercentileNearestRank(accs2, 2.5)
max, _ := stats.PercentileNearestRank(accs2, 97.5)
accNormal = normal
acc5thNormal = min
......@@ -152,6 +154,22 @@ func CalculateStages(ctx common.DawnCtx, request models.StageRequest) map[string
}
func AvgDiff(data []int) float64 {
sort.Ints(data)
sum := 0.0
c := 0
for i := 0; i < len(data)-1; i++ {
diff := math.Abs(float64(data[i] - data[i+1]))
sum += diff
c += 1
}
return sum / float64(c)
}
func Min(data []int) int {
sort.Ints(data)
return data[0]
}
func BinStageMatches(stageState map[string]models.StageStateInner, year int, start int, plantDate time.Time) map[string]models.Bins {
response := map[string]models.Bins{}
alpha := 1.0
......@@ -161,24 +179,13 @@ func BinStageMatches(stageState map[string]models.StageStateInner, year int, sta
}
for state, stateVal := range stageState {
min := stateVal.Normal95thIdx
max := stateVal.Normal5thIdx
// min := stateVal.Normal95thIdx
min := Min(stateVal.Hists)
stepSize := int(math.Ceil(AvgDiff(stateVal.Hists)) + 1) // add 1 to increase range (cheating a little) and for uncertainty
max := min + int(stepSize)*5
rangeSize := max - min
if rangeSize%5 != 0 {
target := math.Ceil(float64(rangeSize)/5) * 5
dist := math.Abs((target - float64(rangeSize)))
move := math.Ceil(dist / 2)
max += int(move)
min -= int(move)
rangeSize = max - min
if rangeSize%5 != 0 {
min -= 1
}
rangeSize = max - min
}
arr := []float64{}
idxs := []int{}
stepSize := rangeSize / 5
base := min
total := 0
for i := 0; i < 5; i++ {
......@@ -209,11 +216,74 @@ func BinStageMatches(stageState map[string]models.StageStateInner, year int, sta
inner.NormalMax = plantDate.AddDate(0, 0, max-start)
inner.NormalMean = plantDate.AddDate(0, 0, stateVal.NormalMeanIdx-start)
inner.Count = total
fmt.Println(state, rangeSize, stepSize, total)
response[state] = inner
}
return response
}
// func BinStageMatches(stageState map[string]models.StageStateInner, year int, start int, plantDate time.Time) map[string]models.Bins {
// response := map[string]models.Bins{}
// alpha := 1.0
// add := 0
// if year%4 == 0 && year%100 != 0 || year%400 == 0 {
// add -= 1
// }
// for state, stateVal := range stageState {
// min := stateVal.Normal95thIdx
// max := stateVal.Normal5thIdx
// rangeSize := max - min
// if rangeSize%5 != 0 {
// target := math.Ceil(float64(rangeSize)/5) * 5
// dist := math.Abs((target - float64(rangeSize)))
// move := math.Ceil(dist / 2)
// max += int(move)
// min -= int(move)
// rangeSize = max - min
// if rangeSize%5 != 0 {
// min -= 1
// }
// rangeSize = max - min
// }
// arr := []float64{}
// idxs := []int{}
// stepSize := rangeSize / 5
// base := min
// total := 0
// for i := 0; i < 5; i++ {
// count := 0.0
// for _, h := range stateVal.Hists {
// if base <= h && h < base+stepSize {
// count += 1
// total += 1
// }
// }
// idxs = append(idxs, base)
// arr = append(arr, count)
// base += stepSize
// }
// inner := models.Bins{}
// inner.Bins = []models.Bin{}
// for i := 0; i < 5; i++ {
// idx := idxs[i] + add
// date := plantDate.AddDate(0, 0, idx-start)
// val := arr[i]
// smoothedVal := (val + alpha) / (float64(total) + 5*alpha) // modified version of laplace smoothing to remove 0%
// inner.Bins = append(inner.Bins, models.Bin{
// Date: date,
// Value: smoothedVal,
// })
// }
// inner.NormalMin = plantDate.AddDate(0, 0, min-start)
// inner.NormalMax = plantDate.AddDate(0, 0, max-start)
// inner.NormalMean = plantDate.AddDate(0, 0, stateVal.NormalMeanIdx-start)
// inner.Count = total
// response[state] = inner
// }
// return response
// }
func ForecastFirstLastFreeze(ctx common.DawnCtx, request models.FreezingForecastRequest) models.FreezingForecastResponse {
lastFreezeIdx := 0
firstFreezeIdx := 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment