p2: add saveStuff as a way to save...stuff

This commit is contained in:
leo 2023-02-25 21:55:09 +01:00
parent bd164878a0
commit e5999e1c39
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 80 additions and 0 deletions

@ -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
}

@ -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