'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
#CustomBuildAction: after packager, %WINDIR%\System32\robocopy.exe, Python temp\build\bin\python /E /XD __pycache__ Doc pip setuptools tests
'Ctrl + click to open Python shell: ide://run?File=%PROJECT%\Objects\Python\WinPython+Command+Prompt.exe
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Public Py As PyBridge
Private SP As SimpleXYPlot
End Sub
Public Sub Initialize
SP.Initialize
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
B4XPages.AddPageAndCreate("SP", SP)
Py.Initialize(Me, "Py")
Dim opt As PyOptions = Py.CreateOptions(File.Combine(File.DirApp, "Python/python/python.exe"))
Py.Start(opt)
Wait For Py_Connected (Success As Boolean)
If Success = False Then
LogError("Failed to start Python process.")
Return
End If
Py.ImportModule("numpy")
'Py.ImportModule("scipy", "stats") 'This doesn't seem to work - I had to put it in the code
Dim x As List = B4XCollections.CreateList(Array(5,7,8,7,2,17,2,9,4,11,12,9,6))
Dim y As List = B4XCollections.CreateList(Array(99,86,87,88,111,86,103,87,94,78,77,85,86))
Wait For (LinearRegression(x, y).Fetch) Complete (results As PyWrapper)
Dim ar() As Object = results.Value
Dim mp As Map = CreateMap("slope": ar(0), "intercept": ar(1), "r": ar(2), "p": ar(3), "std_err": ar(4))
plotResults(x, y, mp)
End Sub
Private Sub B4XPage_Background
Py.KillProcess
End Sub
Private Sub Py_Disconnected
Log("PyBridge disconnected")
End Sub
Private Sub LinearRegression(xs As List, ys As List) As PyWrapper
Dim Code As String = $"
from scipy import stats
# Multiple Linear Regression Module
def LinearRegression (xs, ys):
x = numpy.array(xs)
y = numpy.array(ys)
return stats.linregress(x, y)
"$
Return Py.RunCode("LinearRegression", Array(xs, ys), Code)
End Sub
'draw simple XY plot
Private Sub plotResults(x As List, y As List, mp As Map) 'ignore
SP.drawXY(x, y)
SP.drawRegression(mp.Get("intercept"), mp.Get("slope"))
Dim r As Float = mp.Get("r")
SP.addTitle("Fig 1. Best-fit Line to Data Points")
SP.addSubTitle($"[n=${x.size}; r=$1.3{r}; r${Chr(0x00B2)}=$1.3{Power(r, 2)}; p=$1.3{mp.Get("p")}]"$)
SP.addYLabel("Y - Axis")
SP.addXLabel("X - Axis")
B4XPages.ShowPage("SP")
End Sub