comparison pkg/controllers/gauges.go @ 4087:1562a5fb36dc

Handle Nash-Sutcliff more tolerant for NaN/Inf values. Due to limitation of JSON we encode these values as 0 and +/- MaxFloat64.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Jul 2019 17:15:00 +0200
parents 18777f6df3ef
children 472aedc8927d
comparison
equal deleted inserted replaced
4082:419f28898db0 4087:1562a5fb36dc
634 predicted = append(predicted, p) 634 predicted = append(predicted, p)
635 observed = append(observed, values[j].observed) 635 observed = append(observed, values[j].observed)
636 } 636 }
637 } 637 }
638 638
639 cs[i].Value = common.NashSutcliffe(predicted, observed) 639 cs[i].Value = sanitizeFloat64(common.NashSutcliffe(predicted, observed))
640 cs[i].Samples = len(predicted) 640 cs[i].Samples = len(predicted)
641 641
642 predicted = predicted[:0] 642 predicted = predicted[:0]
643 observed = observed[:0] 643 observed = observed[:0]
644 } 644 }
648 When: models.ImportTime{Time: when}, 648 When: models.ImportTime{Time: when},
649 Coeffs: cs, 649 Coeffs: cs,
650 }, 650 },
651 } 651 }
652 return 652 return
653 }
654
655 func sanitizeFloat64(x float64) float64 {
656 switch {
657 case math.IsNaN(x):
658 return 0
659 case math.IsInf(x, +1):
660 return math.MaxFloat64
661 case math.IsInf(x, -1):
662 return -math.MaxFloat64
663 }
664 return x
653 } 665 }
654 666
655 func waterlevels(rw http.ResponseWriter, req *http.Request) { 667 func waterlevels(rw http.ResponseWriter, req *http.Request) {
656 gauge := mux.Vars(req)["gauge"] 668 gauge := mux.Vars(req)["gauge"]
657 669