You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

factorset_upload.go 2.0 kB

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