From e5999e1c39277a89ce56326bd220911ea199eeb5 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 25 Feb 2023 21:55:09 +0100 Subject: [PATCH] p2: add saveStuff as a way to save...stuff --- p2/data.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ p2/run.go | 10 ++++++++ 2 files changed, 80 insertions(+) diff --git a/p2/data.go b/p2/data.go index 22bcdeb..476ae09 100644 --- a/p2/data.go +++ b/p2/data.go @@ -79,3 +79,73 @@ func readFile(s *string) ([][]float64, error) { return data, nil } + +func saveStuff( + meanU, meanY, + varianceU, varianceY float64, + autocorrelationU, autocorrelationY, + mutCorrelationUY, mutCorrelationYU []float64, +) error { + fFnames := map[string]float64{ + "mean_u": meanU, + "mean_y": meanY, + "variance_u": varianceU, + "variance_y": varianceY, + } + sFnames := map[string][]float64{ + "autocorrelation_u": autocorrelationU, + "autocorrelation_y": autocorrelationY, + "mutual_correlation_uy": mutCorrelationUY, + "mutual_correlation_yu": mutCorrelationYU, + } + prefix := "data/" + suffix := ".txt" + + for k, v := range fFnames { + f, err := os.Create(prefix + k + suffix) + if err != nil { + return err + } + + defer f.Close() + + _, err = f.WriteString(strconv.FormatFloat(v, 'f', 64, 64)) + if err != nil { + return err + } + } + + suffix = ".csv" + + for k, v := range sFnames { + f, err := os.Create(prefix + k + suffix) + if err != nil { + return err + } + + defer f.Close() + + w := csv.NewWriter(f) + + s := recordsFromFloat64Slice(v) + + err = w.WriteAll(s) + if err != nil { + return err + } + } + + return nil +} + +func recordsFromFloat64Slice(f []float64) [][]string { + s := make([][]string, 0, len(f)) + + for i := range f { + tmp := strconv.FormatFloat(f[i], 'f', 64, 64) + + s = append(s, []string{tmp}) + } + + return s +} diff --git a/p2/run.go b/p2/run.go index 986dca4..5c5e965 100644 --- a/p2/run.go +++ b/p2/run.go @@ -51,6 +51,16 @@ func run() error { log.Printf("autocorrelationY: %v", autocorrelationY) log.Printf("mutual correlation U,Y: %v", mutCorrelationUY) log.Printf("mutual correlation Y,U: %v", mutCorrelationYU) + + err = saveStuff( + meanU, meanY, + varianceU, varianceY, + autocorrelationU, autocorrelationY, + mutCorrelationUY, mutCorrelationYU, + ) + if err != nil { + return err + } } return nil