GithubHelp home page GithubHelp logo

00mjk / pjlink Goto Github PK

View Code? Open in Web Editor NEW

This project forked from b3m2a1/pjlink

0.0 0.0 0.0 5.8 MB

JLink but for python: a link between Mathematica and python with efficient data transfer

License: MIT License

Python 56.18% C++ 27.36% Mathematica 16.46% Shell 0.01%

pjlink's Introduction

PJLink has been superseded by the Wolfram Client for Python

Please use that if you can. If that is insufficient for one reason or another raise an issue there and if they refuse to fix it let me know and I'll try to add it to PJLink when I have the time and energy to.


PJLink

version license

PJLink is a package developed to link python and Mathematica. It is currently python 3.3+ only as it turns out to be more challenging to support legacy python2.7 as well in the C-interface than I would currently like, although it will likely be less difficult to do so in the python codebase.


Installation

The easiest way to install PJLink is using a paclet server installation:

PacletInstall[
  "PJLink",
  "Site"->
    "http://www.wolframcloud.com/objects/b3m2a1.paclets/PacletServer"
  ]

If you've already installed it you can update using:

PacletUpdate[
  "PJLink",
  "Site"->
    "http://www.wolframcloud.com/objects/b3m2a1.paclets/PacletServer"
  ]

Alternately you can download this repo as a ZIP file and put extract it in $UserBaseDirectory/Applications


Usage

Before anything else, we'll load the package:

<<PJLink`

To start you need to call InstallPython to load a python runtime (the first time this is done it may be slow as it compiles the library PJLink uses for communication):

ker=InstallPython[];

After that you can evaluate python code as a string

PyEvaluateString["import numpy as np"];
PyEvaluateString["np.random.rand(5, 5, 5)"]~Short~3
(*Out:*)

{{{0.19340495587982665`,0.04333471882108819`,0.0793674077113492`,0.6746465215828963`,0.9128377509416972`},<<3>>,{0.36109338142334146`,0.44378058193673864`,<<19>>,<<21>>,0.5244287636275622`}},<<3>>,{<<1>>}}

You can also using the PJLink symbolic processing system to evaluate python code directly from Mathematica code. This is the best way to pass data into python:

With[{arr=RandomReal[{-1, 1}, {100, 100,100}]}, PyEvaluate[test=arr,  TimeConstraint->1]]//AbsoluteTiming//First
(*Out:*)

0.102152`
PyEvaluate[test]//Dimensions//AbsoluteTiming
(*Out:*)

{0.020897`,{100,100,100}}

Errors will be returned wrapped in PythonTraceback :

PyEvaluate[nosym]
(*Out:*)

PythonTraceback["Traceback (most recent call last):\n  File \"~/Documents/Python/IDEA/PJLink/PJLink/KernelLink.py\", line 654, in __handleCallPacket\n    self.__callPython()\n  File \"/Users/Mark/Documents/Python/IDEA/PJLink/PJLink/KernelLink.py\", line 1003, in __callPython\n    res = self.__do_call_recursive(pkt)\n File \"~/Documents/Python/IDEA/PJLink/PJLink/KernelLink.py\", line 959, in __do_call_recursive\n    res = eval(arg, self.__EXEC_ENV, self.__EXEC_ENV)\n  File \"<string>\", line 1, in <module>\nNameError: name 'nosym' is not defined\n"]

When done, call ClosePython to clean up the runtime and close the link:

ClosePython[]

You can also use this package to communicate with Mathematica from Python as I detailed here .

pjlink's People

Contributors

b3m2a1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.