Skip to main content

How to send packed array from math kernel to 3rd party app via WSTP


Background



I'm developing a small python wrapper PyWSTP. The wrapper is still in very early stage and lacking features. One important feature I'm considering is transferring dense array between math kernel and python. (Yep, the all awesome NumPy) I do realize LibraryLink would be better suited for this, but WSTP has the capability of sending stuff over TCP.


Problem


Whenever kernel sends packed array to a 3rd party app via LinkWrite[_LinkObject, _], it sends unpacked array. On the receiving side, tokens such WSTKPACKED are never seen. Thus WSTP C APIs such as WSGetReal64Array cannot be used.


Here's some simple code to illustrate the problem:


server.m


(* run via "math -script" *)
Needs["Developer`"];

link = LinkCreate["testlink", LinkMode->Listen];


obj1 = Image[RandomInteger[{0, 255}, {4, 4}]];
obj2 = Developer`ToPackedArray[RandomReal[1., {4,4}]];
LinkWrite[link, #]& /@ {obj1, obj2};
InputString["Done, ENTER to exit ..."];

LinkClose[link];

client.py


import wstp


ctx = wstp.WSTPContext()
link = ctx.open_link('testlink', mode='connect')

obj1 = link.recv()
print('got object1:')
print(obj1)
obj2 = link.recv()
print('got object2:')
print(obj2)
input('Done, ENTER to exit ...')


ctx.close_link('testlink')

On PyWSTP, I have not implemented handlers for WSTKPACKED yet. Only basic data types (WSTKSYM, WSTKFUNC, WSTKINT, WSTKREAL, WSTKSTR) are implemented. Yet client side is able to correctly receive the object:


got object1:
Image[RawArray["Real64", List[List[207.0, 142.0, 166.0, 205.0], List[137.0, 115.0, 60.0, 39.0], List[37.0, 110.0, 9.0, 89.0], List[56.0, 162.0, 116.0, 225.0]]], "Real", Rule[ColorSpace, Automatic], Rule[Interleaving, None]]
got object2:
List[List[0.8681530207587866, 0.0734728851659936, 0.9233463976844913, 0.1346824422121633], List[0.7711652733626824, 0.88933420636346, 0.9202474236790081, 0.5288719560153152], List[0.18096422954894353, 0.04400711306110572, 0.2330108388845873, 0.7479540642843265], List[0.4382030298759463, 0.42470372650663424, 0.9718400392443247, 0.6100416353164417]]
Done, ENTER to exit ...


This indicates math kernel unpacks dense array before sending over WSTP.


Question summary




  1. How to make sure kernel sends packed array to 3rd party app over WSTP?




  2. Is it possible to achieve 1. without writing C code?







Comments

Popular posts from this blog

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 - 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 - 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....