-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve_fit.go
82 lines (67 loc) · 1.87 KB
/
curve_fit.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
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"os"
"path"
"runtime"
"sync"
"github.com/M-Quadra/go-python3-submodule/v11/py"
pyeval "github.com/M-Quadra/go-python3-submodule/v11/py-eval"
pyfloat "github.com/M-Quadra/go-python3-submodule/v11/py-float"
pygilstate "github.com/M-Quadra/go-python3-submodule/v11/py-gil-state"
pyimport "github.com/M-Quadra/go-python3-submodule/v11/py-import"
pylist "github.com/M-Quadra/go-python3-submodule/v11/py-list"
pyobject "github.com/M-Quadra/go-python3-submodule/v11/py-object"
pysys "github.com/M-Quadra/go-python3-submodule/v11/py-sys"
pyunicode "github.com/M-Quadra/go-python3-submodule/v11/py-unicode"
)
func init() {
py.Finalize()
py.Initialize()
if !py.IsInitialized() {
os.Exit(-1)
}
paths := pysys.GetObject("path")
if paths == nil {
os.Exit(-1)
}
_, file, _, ok := runtime.Caller(0)
if !ok {
os.Exit(-1)
}
wd := path.Dir(file)
wdUnicode := pyunicode.FromString(wd)
defer py.DecRef(wdUnicode)
if !pylist.Append(paths, wdUnicode) {
os.Exit(-1)
}
}
var _m = sync.Mutex{}
// GetPopt python function
func GetPopt(trainX, trainY []int) []float64 {
_m.Lock()
defer _m.Unlock()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if !pygilstate.Check() {
save := pyeval.SaveThread()
defer pyeval.RestoreThread(save)
gstate := pygilstate.Ensure()
defer pygilstate.Release(gstate)
}
trainXPy := pylist.FromInts(trainX)
defer py.DecRef(trainXPy)
trainYPy := pylist.FromInts(trainY)
defer py.DecRef(trainYPy)
curvefit := pyimport.ImportModule("curvefit")
defer py.DecRef(curvefit)
getPopt := pyobject.GetAttrString(curvefit, "getPopt")
defer py.DecRef(getPopt)
res := pyobject.CallFunctionObjArgs(getPopt, trainXPy, trainYPy)
defer py.DecRef(res)
opt := make([]float64, 0, pylist.Size(res))
for i := 0; i < pylist.Size(res); i++ {
item := pyfloat.AsFloat64(pylist.GetItem(res, i))
opt = append(opt, item)
}
return opt
}