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

optimize

parent 0b38a7f8
No related branches found
No related tags found
No related merge requests found
...@@ -58,10 +58,14 @@ func getDatesForCfsMatches(cfs [][]float64, lastDateInt int, currentMatch int, k ...@@ -58,10 +58,14 @@ func getDatesForCfsMatches(cfs [][]float64, lastDateInt int, currentMatch int, k
return out return out
} }
// bin array of dates into a frequency map and return that map and keys // bin array of dates into a frequency map
func bin(data []int) (map[int]int, []int) { //
// returns: frequency map, keys, and the key with the highest frequency
func bin(data []int) (map[int]int, []int, int) {
out := map[int]int{} out := map[int]int{}
keys := []int{} keys := []int{}
maxKey := 0
max := 0
for _, d := range data { for _, d := range data {
if count, exists := out[d]; exists { if count, exists := out[d]; exists {
out[d] = count + 1 out[d] = count + 1
...@@ -69,21 +73,12 @@ func bin(data []int) (map[int]int, []int) { ...@@ -69,21 +73,12 @@ func bin(data []int) (map[int]int, []int) {
out[d] = 1 out[d] = 1
keys = append(keys, d) keys = append(keys, d)
} }
} if out[d] > max {
return out, keys max = out[d]
} maxKey = d
// get the highest frequency bin
func getLargestBinCount(bins map[int]int) int {
largest := 0
largestKey := 0
for key, count := range bins {
if count > largest {
largest = count
largestKey = key
} }
} }
return largestKey return out, keys, maxKey
} }
// get keys of the stage matches in sorted order // get keys of the stage matches in sorted order
...@@ -161,9 +156,8 @@ func forecast(ctx common.DawnCtx, gddReq models.GddRequest, plantdate time.Time, ...@@ -161,9 +156,8 @@ func forecast(ctx common.DawnCtx, gddReq models.GddRequest, plantdate time.Time,
// this loop will actually build the 5 bins // this loop will actually build the 5 bins
for k, v := range cfsHist { for k, v := range cfsHist {
binnedDates, keys := bin(v) binnedDates, keys, largestKey := bin(v)
stepSize := int(math.Ceil(AvgDiff(keys)) + 1) // add 1 to increase range and for uncertainty stepSize := int(math.Ceil(AvgDiff(keys)) + 1) // add 1 to increase range and for uncertainty
largestKey := getLargestBinCount(binnedDates)
sort.Ints(keys) sort.Ints(keys)
...@@ -333,14 +327,13 @@ func CalculateStages(ctx common.DawnCtx, request models.StageRequest) map[string ...@@ -333,14 +327,13 @@ func CalculateStages(ctx common.DawnCtx, request models.StageRequest) map[string
Accumulate: false, Accumulate: false,
Product: "CORN", Product: "CORN",
} }
gdds, cfs := asyncCollectGddsAndCfs(ctx, gddReq)
stageMatches := models.BuildStageMatches(request.Mode, request.Value) stageMatches := models.BuildStageMatches(request.Mode, request.Value)
comparisonThread := dispatch.New[map[string]time.Time]() comparisonThread := dispatch.New[map[string]time.Time]()
go comparisonGoRoutine(gddReq, request.PlantDate, stageMatches, request.Comparison, comparisonThread) go comparisonGoRoutine(gddReq, request.PlantDate, stageMatches, request.Comparison, comparisonThread)
gdds, cfs := asyncCollectGddsAndCfs(ctx, gddReq)
out := forecast(ctx, gddReq, request.PlantDate, stageMatches, gdds, cfs) out := forecast(ctx, gddReq, request.PlantDate, stageMatches, gdds, cfs)
// block, wait for comparison results // block, wait for comparison results
......
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