Skip to main content

compile - How to execute an existing notebook with Wolfram Engine?


[My related question: mwe-for-compiling-functions-into-standalone-dll-and-calling-them-in-python - here I am exploring an alternative]


I have downloaded, installed and activated the wolfram engine (12.0) on Windows, and I have installed the WolframClient in my Python environment.


In python I execute


from wolframclient.language import wl
from wolframclient.evaluation import WolframLanguageSession

kernelLoc = 'C:\\Program Files\\Wolfram Research\\Wolfram Engine\\12.0\\WolframKernel.exe'
session = WolframLanguageSession(kernelLoc)
session.evaluate(wl.StringReverse('abc'))

and the result is 'cba' proving that the integration is working.


I have a workbook that contains all my (valid) compilable functions.


Question: how do I load & evaluate that notebook and call the compiled functions? (I could use a package file instead if that would help)


I have not (yet) found the answer by reference to the Wolfram Client documentation


Side issue: I recall that setting up my MMA 11.0.1.0 to work with Visual Studio 2017 for C compilation was... tricky. Does the Wolfram Engine have independent setup and if so how do I apply the working config for compilation from MMA to WE? Of course, if I can load the required notebook and it just works then this becomes redundant, but... expect the not-to-be-unexpected is my motto for development.


Update



Loading a Notebook requires FrontEnd, which is not available; I tried to import using NotebookImport, passing a filename and using "Input" as the style filter but also to no avail


using


nbPath = 'A:\\My Documents\\...\\ctXi_WolframEngine.nb'
mynb = session.evaluate(wl.NotebookImport(nbPath,'Input'))
mynb

I got this, which is clearly not helpful


(HoldComplete[None],
HoldComplete[None],
HoldComplete[None],

HoldComplete[None],
Failure['InterpretationFailure', {'CellStyle': 'Input', 'ContentData': BoxData, 'Cell': RawBoxes[Cell[BoxData['~'], 'Input', Rule[CellChangeTimes, (3771391298.009615,)]]]}],
HoldComplete[None],
...

Note that the notebook works perfectly well in Mathematica itself.



Answer



Provided that $Path contains an appropriate path to a package CTMisc.m the following works


session.evaluate(wlexpr('Needs["CTMisc`"]'))


Then I was able to successfully call a function yyyymmddhhmmssmsDateString[milliseconds_] defined in that package


t = session.evaluate(wlexpr('yyyymmddhhmmssmsDateString[3407800000]'))
print(t)

1900-02-09 10:36:40.000

One must be careful with the expression of strings: contexts as strings are usually written as "MyContext`" in Mathematica. I had a problem when I was trying to be too literal and declared


pkgName = '"MyContext`"'

Specified correctly one can also write e.g.



session.evaluate(wl.Get("MyContext`"))

The following function was defined in the package:


compiledTestFunction = 
Compile[{{n, _Real}, {m, _Real}},
Module[{},
Return[n * m]
];
CompilationTarget->"C", "RuntimeOptions"->"Speed"];


After Get-ting the package we define the Python function and then execute


compiledTestFunctionPython = session.function('compiledTestFunction')
compiledTestFunctionPython(2.0, 7.0)

and the result is as expected, 14.0


Conclusion: Wolfram Engine on Windows 10 with Visual Studio 2017 worked out of the box for C compilation on this MWE.


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.