openbrain/object/vectorset_upload.go

58 lines
1.4 KiB
Go
Raw Normal View History

2022-04-10 10:59:17 +08:00
package object
import (
"fmt"
2022-04-10 22:05:50 +08:00
"strings"
2022-04-10 10:59:17 +08:00
2022-04-16 20:53:36 +08:00
"github.com/openbrainorg/openbrain/util"
2022-04-10 10:59:17 +08:00
)
func (vectorset *Vectorset) LoadVectors(pathPrefix string) {
path := util.GetUploadFilePath(fmt.Sprintf("%s%s", pathPrefix, vectorset.FileName))
2022-04-10 22:05:50 +08:00
var nameArray []string
var dataArray [][]float64
if strings.HasSuffix(vectorset.FileName, ".csv") {
2022-04-12 23:22:46 +08:00
if strings.Contains(vectorset.FileName, "_Dim_") {
nameArray, dataArray = util.LoadVectorFileByCsv2(path)
} else {
nameArray, dataArray = util.LoadVectorFileByCsv(path)
}
2022-04-10 22:05:50 +08:00
} else {
nameArray, dataArray = util.LoadVectorFileBySpace(path)
}
2022-04-10 10:59:17 +08:00
exampleVectors := []*Vector{}
2022-04-12 23:09:24 +08:00
vectors := []*Vector{}
2022-04-10 23:01:25 +08:00
vectorMap := map[string]*Vector{}
for i := 0; i < len(nameArray); i++ {
2022-04-10 10:59:17 +08:00
vector := &Vector{
2022-04-10 21:51:42 +08:00
Name: nameArray[i],
Data: dataArray[i],
2022-04-10 10:59:17 +08:00
}
2022-04-10 23:01:25 +08:00
if i < 100 {
exampleVectors = append(exampleVectors, vector)
}
2022-04-12 23:09:24 +08:00
vectors = append(vectors, vector)
2022-04-10 23:01:25 +08:00
vectorMap[vector.Name] = vector
2022-04-10 10:59:17 +08:00
}
vectorset.Vectors = exampleVectors
2022-04-12 23:09:24 +08:00
vectorset.AllVectors = vectors
2022-04-10 23:01:25 +08:00
vectorset.VectorMap = vectorMap
2022-04-10 10:59:17 +08:00
}
2022-04-12 23:09:24 +08:00
func (vectorset *Vectorset) WriteVectors(pathPrefix string) {
path := util.GetUploadFilePath(fmt.Sprintf("%s%s", pathPrefix, vectorset.FileName))
rows := [][]string{}
for _, vector := range vectorset.AllVectors {
row := util.FloatsToStrings(vector.Data)
2022-04-12 23:22:46 +08:00
row = append([]string{vector.Name}, row...)
2022-04-12 23:09:24 +08:00
rows = append(rows, row)
}
util.WriteCsvFile(path, &rows)
}