Message Boards Message Boards

How to pass data to Python in Mathematica?

I hope to use the Python package to assist in data calculations. Can I pass data to Python in Mathematica? I have tried to save the data as a file and then read it with NumPy (this is feasible)~~ But I still hope to be able to directly transfer data to improve efficiency!

POSTED BY: Tsai Ming-Chou
5 Replies

Thanks to Rohit Namjoshi and Alec Graves for their suggestions. It helped me a lot!!

session = StartExternalSession["Python"];
ExternalEvaluate[session, {"from pykrige.uk import UniversalKriging", 
   "import numpy as np", "import matplotlib.pyplot as plt"}];
(*test is data in Python;rangeX is gridX in Python;rangeY is gridy in 
Python*)
test = {{120.409653, 23.925175, 1.}, {120.758833, 24.382942, 
    1.}, {121.451861, 24.982528, 16.}, {121.5145, 25.105917, 
    13.}, {121.513311, 25.0632, 8.}, {120.677689, 24.099611, 
    5.}, {121.201811, 25.060344, 13.}, {120.425311, 22.564136, 
    9.}, {120.337736, 22.565833, 20.}, {121.526528, 25.062361, 
    15.}, {120.332631, 22.689056, 5.}, {120.544994, 23.711853, 
    6.}, {121.529556, 25.020608, 21.}, {120.292917, 22.674861, 
    2.}, {121.204981, 24.954208, 11.}, {120.218333, 23.048333, 
    1.}, {120.24735, 23.465308, 3.}, {121.088903, 24.740644, 
    11.}, {120.616917, 24.162197, 2.}, {120.568794, 24.225628, 
    0.}, {120.641092, 24.151958, 3.}, {121.365703, 25.07857, 
    10.}, {120.41175, 22.4795, 6.}, {121.458667, 25.012972, 
    19.}, {121.578611, 25.05, 14.}, {120.288086, 22.632567, 
    1.}, {120.307564, 22.605386, 3.}, {120.685306, 23.913, 
    12.}, {120.488033, 22.673081, 4.}, {120.530542, 22.883583, 
    4.}, {120.8202, 24.565269, 3.}, {121.304417, 24.995667, 
    15.}, {121.760056, 25.129167, 9.}, {120.348742, 23.757547, 
    2.}, {121.449239, 25.1645, 14.}, {121.038653, 24.900142, 
    5.}, {121.481028, 25.06895, 12.}, {120.972075, 24.805619, 
    15.}, {121.537778, 24.977222, 25.}, {121.4325, 25.037972, 
    15.}, {120.345531, 23.554839, 2.}, {120.31725, 23.305633, 
    3.}, {120.328289, 22.733667, 0.}, {121.689881, 25.179667, 
    10.}, {121.507972, 25.046503, 11.}, {120.541519, 24.066, 
    3.}, {120.358083, 22.627392, 8.}, {120.561175, 22.523108, 
    0.}, {120.469061, 24.131672, 1.}, {120.305689, 22.757506, 
    5.}, {120.898572, 24.696969, 1.}, {121.21635, 24.863869, 
    5.}, {120.741711, 24.256586, 2.}, {121.082761, 25.035503, 
    12.}, {120.202842, 23.717533, 3.}, {120.202617, 22.984581, 2.}};

rangeX = Range[#[[1]], #[[2]], 0.05] &@MinMax@test[[All, 1]];
rangeY = Range[#[[1]], #[[2]], 0.05] &@MinMax@test[[All, 2]];
(*This is the key point.*)
(*Transfer data from Mathematica to Python*)
ExternalEvaluate[session, {"data=np.array(<*test*>)", "gridx=np.array(<*rangeX*>)", 
   "gridy=np.array(<*rangeY*>)"}];
(*Confirm that the data has been Transferred from Mathematica to 
Python*)
ExternalEvaluate[session, {"print(data)", "print(gridx)", "print(gridy)"}];
(*Create the universal kriging object*)
ExternalEvaluate[session, {"UK=UniversalKriging(data[:,0], data[:,1], data[:,2], 
variogram_model='linear', drift_terms=['regional_linear'])"}];
(*Creates the kriging grid and the variance grid*)
ExternalEvaluate[session, {"z, ss = UK.execute('grid', gridx, gridy)", 
   "plt.imshow(z)", "plt.show()"}];
DeleteObject[session];

The result of the execution: enter image description here

POSTED BY: Tsai Ming-Chou
Posted 3 years ago

Direct transfer of data to a Python environment is absolutely possible using ExternalEvaluate.

See this simple example of using the numpy library with your test data (note that "Arguments" is expecting a list):

This is surprisingly fast, with data transfer executing in 10ms on my MacBook Air 2019.

POSTED BY: Alec Graves
Posted 3 years ago

Hi Ming-Chou,

Assuming that the data is generated using some Wolfram Language code, it might be easier to call it from Python. Take a look at the Wolfram Client Library for Python.

POSTED BY: Rohit Namjoshi

Thank you for your suggestion~~ I tried it according to my understanding~ But received the wrong message ("WLSymbolFactory object is not subscriptable") Could you please give more suggestions?

POSTED BY: Tsai Ming-Chou
Posted 3 years ago

The problem is that data in Python is formatted as a WL List, not a Python array. I am not sure what is the best way to do the conversion. You could try

testArray = ExportString[test, "PythonExpression"]

and convert testArray from string to Python array using eval. There must be a better way.

You mentioned earlier that writing to a file in WL and reading it from Python works but you were looking for a more efficient method. How many geo locations are there in your real data?

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract