func main() { // create file storing ks and vard f, err := os.Create(fname + ".csv") if err != nil { panic(err) } defer f.Close() ksarray := []float64{} // store ks vdarray := []float64{} // store VarD ngarray := []float64{} // store generation number // do evolution for i := 0; i < ngen; i++ { pop.Evolve() // we make 10 samples and average ksmean := desc.NewMean() vdmean := desc.NewMean() for j := 0; j < sampleTime; j++ { sample := fwd.Sample(pop.Genomes, sampleSize) dmatrix := fwd.GenerateDistanceMatrix(sample) cmatrix := covs.NewCMatrix(sampleSize, pop.Length, dmatrix) ks, vard := cmatrix.D() ksmean.Increment(ks) vdmean.Increment(vard) } // write to csv f.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult())) if (i+1)%1000 == 0 { fmt.Println("Generation: ", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult()) } // store array for draw ksarray = append(ksarray, ksmean.GetResult()) vdarray = append(vdarray, vdmean.GetResult()) ngarray = append(ngarray, float64(pop.NumOfGen)) } // draw svger := render.NewSVG(fname, 1, 2, 800, 200) pl := chart.ScatterChart{Title: "KS"} pl.AddDataPair("KS", ngarray, ksarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "VarD"} pl.AddDataPair("VarD", ngarray, vdarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() // save population jf, err := os.Create(fname + ".json") if err != nil { panic(err) } defer jf.Close() b, err := json.Marshal(pop) if err != nil { panic(err) } jf.Write(b) }
func (s *Statistics) Add(c *chart.ScatterChart, name, col string, eval func() float64) { xs, ys := []float64{}, []float64{} rgbac := color.RGBAModel.Convert(hexcolor.Hex(col)) c.AddDataPair(name, xs, ys, chart.PlotStyleLines, chart.Style{LineColor: rgbac, LineWidth: 1}) *s = append(*s, Statistic{Eval: eval}) for i := range c.Data { (*s)[i].Samples = &c.Data[i].Samples } }
func (s *Schedule) ExprGraph(t miniprofiler.Timer, unit string, res []*expr.Result) (chart.Chart, error) { c := chart.ScatterChart{ Key: chart.Key{Pos: "itl"}, YRange: chart.Range{Label: unit}, } c.XRange.Time = true for ri, r := range res { rv := r.Value.(expr.Series) pts := make([]chart.EPoint, len(rv)) idx := 0 for k, v := range rv { pts[idx].X = float64(k.Unix()) pts[idx].Y = v idx++ } slice.Sort(pts, func(i, j int) bool { return pts[i].X < pts[j].X }) c.AddData(r.Group.String(), pts, chart.PlotStyleLinesPoints, Autostyle(ri)) } return &c, nil }
// the converge of KS equalibrium. func main() { ksarray := []float64{} // store ks vdarray := []float64{} // store VarD ngarray := []float64{} // store generation number // do evolution for i := 0; i < ngen; i++ { pop.Evolve() // select 10 samples and averge mean := desc.NewMean() vmean := desc.NewMean() ch := make(chan dResult) num := 10 for j := 0; j < num; j++ { sample := fwd1.Sample(pop.Genomes, sampleSize) go calculateD(sample, pop.Length, ch) } for j := 0; j < num; j++ { dr := <-ch mean.Increment(dr.ks) vmean.Increment(dr.vard) } ksarray = append(ksarray, mean.GetResult()) vdarray = append(vdarray, vmean.GetResult()) ngarray = append(ngarray, float64(pop.NumOfGen)) } // draw svger := render.NewSVG(fname, 1, 2, 800, 200) pl := chart.ScatterChart{Title: "KS"} pl.AddDataPair("KS", ngarray, ksarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "VarD"} pl.AddDataPair("VarD", ngarray, vdarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() }
func campaign() { x := []float64{0, 1, 2, 3, 4, 5, 6} const plotStyle = chart.PlotStyleLines const lineStyle = chart.SolidLine dumper := NewDumper("scatter", 2, 1, 450, 400) defer dumper.Close() TTFBLow := []float64{40, 55, 40, 55, 122, 145, 166} // Categorized Bar Chart c := chart.ScatterChart{} c.YRange.Label = "Pro-EU" c.XRange.Label = "Succesfuld karriere" c.XRange.Category = months // Et klogt dyr bider ej hånden som føder den. // Magt fører mere magt med sig. Stå i opposition til magten, og magten har ej brug for dig. c.AddDataPair("Politiker", x, TTFBLow, plotStyle, chart.Style{LineColor: turquise, LineStyle: chart.LongDashLine}) dumper.Plot(&c) // Du tjener systemet. Systemet belønner dig. Skønt tusind år forgår, forbliver menneskelig natur altid den samme. // //Det er lettere at gå i medvind, en at gå imod vinden og den herskende ånd. // Den der går imod vinden, går fattig, udskældt og ene. TTFBFileHigh20 := []float64{160, 135, 177, 130, 150, 250, 260} // Categorized Bar Chart c = chart.ScatterChart{} c.YRange.Label = "Idealisme" c.XRange.Label = "Karrierens længde" c.XRange.Category = months c.AddDataPair("Arbejdsløs", x, TTFBFileHigh20, plotStyle, chart.Style{Symbol: '#', LineColor: red, LineStyle: lineStyle}) dumper.Plot(&c) }
func main() { // ks file ksfilestr := dir + "/" + prex + "_ks.txt" ksfile, err := os.Create(ksfilestr) if err != nil { log.Fatalf("Can not create file: %s, %v", ksfilestr, err) } defer ksfile.Close() ksMean := desc.NewMean() ksVar := desc.NewVarianceWithBiasCorrection() vardMean := desc.NewMean() vardVar := desc.NewVarianceWithBiasCorrection() scovsMeanArray := make([]*desc.Mean, maxL) scovsVarArray := make([]*desc.Variance, maxL) rcovsMeanArray := make([]*desc.Mean, maxL) rcovsVarArray := make([]*desc.Variance, maxL) xyPLMeanArray := make([]*desc.Mean, maxL) xyPLVarArray := make([]*desc.Variance, maxL) xsysPLMeanArray := make([]*desc.Mean, maxL) xsysPLVarArray := make([]*desc.Variance, maxL) smXYPLMeanArray := make([]*desc.Mean, maxL) smXYPLVarArray := make([]*desc.Variance, maxL) for i := 0; i < maxL; i++ { scovsMeanArray[i] = desc.NewMean() scovsVarArray[i] = desc.NewVarianceWithBiasCorrection() rcovsMeanArray[i] = desc.NewMean() rcovsVarArray[i] = desc.NewVarianceWithBiasCorrection() xyPLMeanArray[i] = desc.NewMean() xyPLVarArray[i] = desc.NewVarianceWithBiasCorrection() xsysPLMeanArray[i] = desc.NewMean() xsysPLVarArray[i] = desc.NewVarianceWithBiasCorrection() smXYPLMeanArray[i] = desc.NewMean() smXYPLVarArray[i] = desc.NewVarianceWithBiasCorrection() } stepNum := etime / stepSize for i := 0; i < stepNum; i++ { // simulate pop.Evolve(stepSize) // create distance matrix dmatrix := pop.DistanceMatrix(sampleSize) c := covs.NewCMatrix(dmatrix, sampleSize, pop.Length) // calculate ks ks := c.CalculateKS() vard := c.CalculateVarD() ksfile.WriteString(fmt.Sprintf("%d\t%g\t%g\n", pop.Time, ks, vard)) ksMean.Increment(ks) ksVar.Increment(ks) vardMean.Increment(vard) vardVar.Increment(vard) log.Printf("KsMean: %g, KsVar: %g, VardMean: %g, VardVar: %g\n", ksMean.GetResult(), ksVar.GetResult(), vardMean.GetResult(), vardVar.GetResult()) // calculate covs scovs, rcovs, xyPL, xsysPL, smXYPL := c.CalculateCovs(maxL) for j := 0; j < maxL; j++ { scovsMeanArray[j].Increment(scovs[j]) scovsVarArray[j].Increment(scovs[j]) rcovsMeanArray[j].Increment(rcovs[j]) rcovsVarArray[j].Increment(rcovs[j]) xyPLMeanArray[j].Increment(xyPL[j]) xyPLVarArray[j].Increment(xyPL[j]) xsysPLMeanArray[j].Increment(xsysPL[j]) xsysPLVarArray[j].Increment(xsysPL[j]) smXYPLMeanArray[j].Increment(smXYPL[j]) smXYPLVarArray[j].Increment(smXYPL[j]) } svger := render.NewSVG(dir+"/"+prex+"_covs", 1, 5, 800, 200) scovMeans := make([]float64, maxL-1) rcovMeans := make([]float64, maxL-1) xyPLMeans := make([]float64, maxL-1) xsysPLMeans := make([]float64, maxL-1) smXYPLMeans := make([]float64, maxL-1) xs := make([]float64, maxL-1) for j := 1; j < maxL; j++ { xs[j-1] = float64(j) scovMeans[j-1] = scovsMeanArray[j].GetResult() rcovMeans[j-1] = rcovsMeanArray[j].GetResult() xyPLMeans[j-1] = xyPLMeanArray[j].GetResult() xsysPLMeans[j-1] = xsysPLMeanArray[j].GetResult() smXYPLMeans[j-1] = smXYPLMeanArray[j].GetResult() } pl := chart.ScatterChart{Title: "scovs"} pl.AddDataPair("struc covs", xs, scovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "rcovs"} pl.AddDataPair("rate covs", xs, rcovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "xyPL"} pl.AddDataPair("xyPL", xs, xyPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "xsysPL"} pl.AddDataPair("xsysPL", xs, xsysPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) pl = chart.ScatterChart{Title: "smXYPL"} pl.AddDataPair("smXYPL", xs, smXYPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine}) svger.Plot(&pl) svger.Close() // write to file covsfilestr := dir + "/" + prex + "_covs.txt" covsfile, err := os.Create(covsfilestr) if err != nil { log.Fatalf("Can not create file: %s, %v", covsfilestr, err) } for j := 0; j < maxL; j++ { covsfile.WriteString( fmt.Sprintf("%d\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n", j, scovsMeanArray[j].GetResult(), scovsVarArray[j].GetResult(), rcovsMeanArray[j].GetResult(), rcovsVarArray[j].GetResult(), xyPLMeanArray[j].GetResult(), xyPLVarArray[j].GetResult(), xsysPLMeanArray[j].GetResult(), xsysPLVarArray[j].GetResult(), smXYPLMeanArray[j].GetResult(), smXYPLVarArray[j].GetResult(), ), ) } covsfile.Close() } tnow = time.Now() log.Printf("Done at %v\n", tnow) }
// Graph takes an OpenTSDB request data structure and queries OpenTSDB. Use the // json parameter to pass JSON. Use the b64 parameter to pass base64-encoded // JSON. func Graph(t miniprofiler.Timer, w http.ResponseWriter, r *http.Request) (interface{}, error) { j := []byte(r.FormValue("json")) if bs := r.FormValue("b64"); bs != "" { b, err := base64.StdEncoding.DecodeString(bs) if err != nil { return nil, err } j = b } if len(j) == 0 { return nil, fmt.Errorf("either json or b64 required") } oreq, err := opentsdb.RequestFromJSON(j) if err != nil { return nil, err } if ads_v := r.FormValue("autods"); ads_v != "" { ads_i, err := strconv.Atoi(ads_v) if err != nil { return nil, err } if err := oreq.AutoDownsample(ads_i); err != nil { return nil, err } } ar := make(map[int]bool) for _, v := range r.Form["autorate"] { if i, err := strconv.Atoi(v); err == nil { ar[i] = true } } queries := make([]string, len(oreq.Queries)) var start, end string var startT, endT time.Time if s, ok := oreq.Start.(string); ok && strings.Contains(s, "-ago") { startT, err = opentsdb.ParseTime(s) if err != nil { return nil, err } start = strings.TrimSuffix(s, "-ago") } if s, ok := oreq.End.(string); ok && strings.Contains(s, "-ago") { endT, err = opentsdb.ParseTime(s) if err != nil { return nil, err } end = strings.TrimSuffix(s, "-ago") } if start == "" && end == "" { s, sok := oreq.Start.(int64) e, eok := oreq.End.(int64) if sok && eok { start = fmt.Sprintf("%vs", e-s) startT = time.Unix(s, 0) endT = time.Unix(e, 0) if err != nil { return nil, err } } } if endT.Equal(time.Time{}) { endT = time.Now().UTC() } m_units := make(map[string]string) for i, q := range oreq.Queries { if ar[i] { meta, err := schedule.MetadataMetrics(q.Metric) if err != nil { return nil, err } if meta == nil { return nil, fmt.Errorf("no metadata for %s: cannot use auto rate", q) } if meta.Unit != "" { m_units[q.Metric] = meta.Unit } if meta.Rate != "" { switch meta.Rate { case metadata.Gauge: // ignore case metadata.Rate: q.Rate = true case metadata.Counter: q.Rate = true q.RateOptions = opentsdb.RateOptions{ Counter: true, ResetValue: 1, } default: return nil, fmt.Errorf("unknown metadata rate: %s", meta.Rate) } } } queries[i] = fmt.Sprintf(`q("%v", "%v", "%v")`, q, start, end) if !schedule.SystemConf.GetTSDBContext().Version().FilterSupport() { if err := schedule.Search.Expand(q); err != nil { return nil, err } } } var tr opentsdb.ResponseSet b, _ := json.MarshalIndent(oreq, "", " ") t.StepCustomTiming("tsdb", "query", string(b), func() { h := schedule.SystemConf.GetTSDBHost() if h == "" { err = fmt.Errorf("tsdbHost not set") return } tr, err = oreq.Query(h) }) if err != nil { return nil, err } cs, err := makeChart(tr, m_units) if err != nil { return nil, err } if _, present := r.Form["png"]; present { c := chart.ScatterChart{ Title: fmt.Sprintf("%v - %v", oreq.Start, queries), } c.XRange.Time = true if min, err := strconv.ParseFloat(r.FormValue("min"), 64); err == nil { c.YRange.MinMode.Fixed = true c.YRange.MinMode.Value = min } if max, err := strconv.ParseFloat(r.FormValue("max"), 64); err == nil { c.YRange.MaxMode.Fixed = true c.YRange.MaxMode.Value = max } for ri, r := range cs { pts := make([]chart.EPoint, len(r.Data)) for idx, v := range r.Data { pts[idx].X = v[0] pts[idx].Y = v[1] } slice.Sort(pts, func(i, j int) bool { return pts[i].X < pts[j].X }) c.AddData(r.Name, pts, chart.PlotStyleLinesPoints, sched.Autostyle(ri)) } w.Header().Set("Content-Type", "image/svg+xml") white := color.RGBA{0xff, 0xff, 0xff, 0xff} const width = 800 const height = 600 s := svg.New(w) s.Start(width, height) s.Rect(0, 0, width, height, "fill: #ffffff") sgr := svgg.AddTo(s, 0, 0, width, height, "", 12, white) c.Plot(sgr) s.End() return nil, nil } var a []annotate.Annotation warnings := []string{} if schedule.SystemConf.AnnotateEnabled() { a, err = annotateBackend.GetAnnotations(&startT, &endT) if err != nil { warnings = append(warnings, fmt.Sprintf("unable to get annotations: %v", err)) } } return struct { Queries []string Series []*chartSeries Annotations []annotate.Annotation Warnings []string }{ queries, cs, a, warnings, }, nil }
func InitStatsHUD() { plots := chart.ScatterChart{Title: "", Options: glchart.DarkStyle} start := time.Now() l := float64(start.UnixNano()) r := float64(start.Add(2 * time.Second).UnixNano()) plots.XRange.Fixed(l, r, 1e9) plots.YRange.Fixed(0.1, 100, 10) plots.XRange.TicSetting.Tics, plots.YRange.TicSetting.Tics = 1, 1 plots.XRange.TicSetting.Mirror, plots.YRange.TicSetting.Mirror = 2, 2 plots.XRange.TicSetting.Grid, plots.YRange.TicSetting.Grid = 2, 2 plots.YRange.ShowZero = true //plots.XRange.Log = true //plots.YRange.Log = true plots.Key.Pos, plots.Key.Cols = "obc", 3 plots.XRange.TicSetting.Format = func(f float64) string { t := time.Unix(int64(f)/1e9, int64(f)%1e9) return fmt.Sprintf("%.3v", time.Since(t)) } memhelper.GetMaxRSS() var gpufree float64 gpupoll := gpuinfo.PollGPUMemory() go func() { for gpustatus := range gpupoll { gpufree = float64(memhelper.ByteSize(gpustatus.Free()) * memhelper.MiB) } }() statistics := &Statistics{} statistics.Add(&plots, "GPU Free", "#FF9F00", func() float64 { return gpufree }) statistics.Add(&plots, "SpareRAM()", "#ff0000", func() float64 { return float64(SpareRAM() * 1e6) }) statistics.Add(&plots, "MaxRSS", "#FFE240", func() float64 { return float64(memhelper.GetMaxRSS()) }) statistics.Add(&plots, "Heap Idle", "#33ff33", func() float64 { return float64(memstats.HeapIdle) }) statistics.Add(&plots, "Alloc", "#FF6600", func() float64 { return float64(memstats.Alloc) }) statistics.Add(&plots, "Heap Alloc", "#006699", func() float64 { return float64(memstats.HeapAlloc) }) statistics.Add(&plots, "Sys", "#996699", func() float64 { return float64(memstats.Sys) }) statistics.Add(&plots, "System Free", "#3333ff", func() float64 { return float64(SystemFree()) }) statistics.Add(&plots, "nBlocks x 1e6", "#FFCC00", func() float64 { return float64(nblocks * 1e6) }) statistics.Add(&plots, "nDrawn x 1e6", "#9C8AA5", func() float64 { return float64(blocks_rendered * 1e6) }) go func() { top := 0. i := -1 for { time.Sleep(250 * time.Millisecond) max := statistics.Update() if max > top { top = max } i++ if i%4 != 0 { continue } segment := float64(1e9) if time.Since(start) > 10*time.Second { segment = 5e9 } if time.Since(start) > 1*time.Minute { segment = 30e9 } // Update axis limits nr := float64(time.Now().Add(2 * time.Second).UnixNano()) plots.XRange.Fixed(l, nr, segment) plots.YRange.Fixed(-1e9, top*1.1, 500e6) } }() const pw, ph = 640, 480 scalex, scaley := 0.4, 0.5 chart_gfxcontext := glchart.New(pw, ph, "", 10, color.RGBA{}) StatsHUD = func() { glh.With(glh.Matrix{gl.PROJECTION}, func() { gl.LoadIdentity() gl.Translated(1-scalex, scaley-1, 0) gl.Scaled(scalex, scaley, 1) gl.Ortho(0, pw, ph, 0, -1, 1) gl.Translated(0, -50, 0) glh.With(glh.Attrib{gl.ENABLE_BIT}, func() { gl.Disable(gl.DEPTH_TEST) glh.With(&Timer{Name: "Chart"}, func() { plots.Plot(chart_gfxcontext) }) }) }) } // TODO: figure out why this is broken DumpStatsHUD = func() { s2f, _ := os.Create("statshud-dump.svg") mysvg := svg.New(s2f) mysvg.Start(1600, 800) mysvg.Rect(0, 0, 2000, 800, "fill: #ffffff") sgr := svgg.New(mysvg, 2000, 800, "Arial", 18, color.RGBA{0xff, 0xff, 0xff, 0xff}) sgr.Begin() plots.Plot(sgr) sgr.End() mysvg.End() s2f.Close() log.Print("Saved statshud-dump.svg") } //log.Print("InitStatsHUD()") }
func Scatter() { x := []float64{0, 1, 2, 3, 4, 5, 6} const plotStyle = chart.PlotStyleLines const lineStyle = chart.SolidLine dumper := NewDumper("scatter", 2, 1, 450, 400) defer dumper.Close() ping := []float64{1, 3, 5, 9, 22, 1, 56} TTFBLow := []float64{40, 55, 40, 55, 122, 145, 166} TTFBMed := []float64{89, 77, 102, 95, 232, 333, 254} TTFBFile := []float64{133, 55, 77, 20, 99, 102, 69} // Categorized Bar Chart c := chart.ScatterChart{} c.XRange.Label = "Måned" c.YRange.Label = "MS" c.XRange.Category = months c.AddDataPair("TTFB-Med", x, TTFBMed, plotStyle, chart.Style{LineColor: green, LineStyle: lineStyle}) c.AddDataPair("TTFB-Low", x, TTFBLow, plotStyle, chart.Style{LineColor: turquise, LineStyle: chart.LongDashLine}) c.AddDataPair("TTFB-File", x, TTFBFile, plotStyle, chart.Style{LineColor: yellow, LineStyle: chart.LongDashLine}) c.AddDataPair("Ping", x, ping, plotStyle, chart.Style{LineColor: brown, LineStyle: lineStyle}) dumper.Plot(&c) TTFBHigh20 := []float64{200, 300, 900, 1500, 1600, 1300} TTFBHigh20HotHour := []float64{250, 350, 1150, 1900, 1800, 1500} TTFBFileHigh20 := []float64{160, 135, 177, 130, 150, 250, 260} TTFBFileHigh20HotHour := []float64{360, 235, 277, 190, 180, 270, 290} // Categorized Bar Chart c = chart.ScatterChart{} c.XRange.Label = "Måned" c.YRange.Label = "MS" c.XRange.Category = months c.AddDataPair("TTFB-High", x, TTFBHigh20, plotStyle, chart.Style{Symbol: '#', LineColor: green, LineStyle: lineStyle}) c.AddDataPair(" - kl. 17-21", x, TTFBHigh20HotHour, plotStyle, chart.Style{ Symbol: '#', LineColor: violet, LineStyle: chart.LongDashLine}) c.AddDataPair("TTFB-FileHigh", x, TTFBFileHigh20, plotStyle, chart.Style{Symbol: '#', LineColor: red, LineStyle: lineStyle}) c.AddDataPair(" - kl. 17-21", x, TTFBFileHigh20HotHour, plotStyle, chart.Style{Symbol: '#', LineColor: blue, LineStyle: chart.LongDashLine}) dumper.Plot(&c) }