/
factorset_upload.go
71 lines (60 loc) · 1.98 KB
/
factorset_upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"strings"
"github.com/casibase/casibase/util"
)
func (factorset *Factorset) LoadFactors(pathPrefix string) {
path := util.GetUploadFilePath(fmt.Sprintf("%s%s", pathPrefix, factorset.FileName))
var nameArray []string
var dataArray [][]float64
if strings.HasSuffix(factorset.FileName, ".csv") {
if strings.Contains(factorset.FileName, "_Dim_") {
nameArray, dataArray = util.LoadFactorFileByCsv2(path)
} else {
nameArray, dataArray = util.LoadFactorFileByCsv(path)
}
} else {
nameArray, dataArray = util.LoadFactorFileBySpace(path)
}
exampleFactors := []*Factor{}
factors := []*Factor{}
factorMap := map[string]*Factor{}
for i := 0; i < len(nameArray); i++ {
factor := &Factor{
Name: nameArray[i],
Data: dataArray[i],
}
if i < 100 {
exampleFactors = append(exampleFactors, factor)
}
factors = append(factors, factor)
factorMap[factor.Name] = factor
}
factorset.Factors = exampleFactors
factorset.AllFactors = factors
factorset.FactorMap = factorMap
}
func (factorset *Factorset) WriteFactors(pathPrefix string) {
path := util.GetUploadFilePath(fmt.Sprintf("%s%s", pathPrefix, factorset.FileName))
rows := [][]string{}
for _, factor := range factorset.AllFactors {
row := util.FloatsToStrings(factor.Data)
row = append([]string{factor.Name}, row...)
rows = append(rows, row)
}
util.WriteCsvFile(path, &rows)
}