inputData: add shift data parsing
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-02-15 13:20:21 +01:00
parent af27d7a62e
commit 27a0c6dbe3
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 75 additions and 11 deletions

@ -18,8 +18,9 @@ var inputData embed.FS
// LoadInputData loads matrices and shift/rotation/shuffle input data and
// returns them as maps.
func LoadInputData(nx int) map[int][][]float64 {
func LoadInputData(nx int) (map[int][][]float64, map[int][][]float64) {
matrices := make(map[int][][]float64)
shiftData := make(map[int][][]float64)
prefix := "input_data"
@ -28,6 +29,7 @@ func LoadInputData(nx int) map[int][][]float64 {
for _, v := range funcNums {
fnameMatrix := fmt.Sprintf("%s/M_%d_D%d.txt", prefix, v, nx)
fnameShift := fmt.Sprintf("%s/shift_data_%d.txt", prefix, v)
{
matrix, err := inputData.Open(fnameMatrix)
@ -42,9 +44,23 @@ func LoadInputData(nx int) map[int][][]float64 {
matrices[v] = m
}
{
shift, err := inputData.Open(fnameShift)
if err != nil {
log.Printf("shift: could not read %s, err: %q\n", fnameShift, err)
continue
}
return matrices
defer shift.Close()
s := parseShiftData(shift, nx)
shiftData[v] = s
}
}
return matrices, shiftData
}
func parseMatrix(f fs.File, dim int) [][]float64 {
@ -79,3 +95,35 @@ func parseMatrix(f fs.File, dim int) [][]float64 {
return fMatrix
}
func parseShiftData(f fs.File, dim int) [][]float64 {
if f == nil {
log.Fatalln("shift file is nil, exiting...")
}
shrows := make([]string, 0, dim)
fSh := make([][]float64, 0, dim)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
shrows = append(shrows, scanner.Text())
}
for _, r := range shrows {
row := strings.Fields(r)
flrow := make([]float64, 0, len(row))
for _, col := range row {
fl, err := strconv.ParseFloat(col, 64)
if err != nil {
log.Fatalf("parseShiftData: could not parse value to float64, col: %s, error: %q, bailing...", col, err)
}
flrow = append(flrow, fl)
}
fSh = append(fSh, flrow)
}
return fSh
}

@ -10,18 +10,34 @@ import (
func TestLoadInputData(t *testing.T) {
dim := 10
fnum := 1
m := LoadInputData(dim)
want := -0.6013070130189602
got := m[fnum][0][0]
m, s := LoadInputData(dim)
wantM := -0.6013070130189602
gotM := m[fnum][0][0]
if want != got {
t.Errorf("unexpected result, want: %v, got: %v", want, got)
// test matrix data.
if wantM != gotM {
t.Errorf("unexpected result, want: %v, got: %v", wantM, gotM)
}
want = 9.2845625214477390e-01
got = m[fnum][1][1]
wantM = 9.2845625214477390e-01
gotM = m[fnum][1][1]
if want != got {
t.Errorf("unexpected result, want: %v, got: %v", want, got)
if wantM != gotM {
t.Errorf("unexpected result, want: %v, got: %v", wantM, gotM)
}
// test shiftData.
wantS := -70.42955971808618
gotS := s[fnum][0][1]
if wantS != gotS {
t.Errorf("unexpected result, want: %v, got: %v", wantS, gotS)
}
wantS = 36.645777950364064
gotS = s[fnum][0][10]
if wantS != gotS {
t.Errorf("unexpected result, want: %v, got: %v", wantS, gotS)
}
}