Group Abstract Group Abstract

Message Boards Message Boards

PJLink: Hooking up Mathematica and Python

Posted 7 years ago
POSTED BY: b3m2a1 ​ 
5 Replies

Thank you! This has been a much needed addition for the last several years. As you point out, the structure of the Wolfram language is intellectually a delight, but one still must be part of a community. The silo approach leaves many of us feeling isolated. This is especially true now with the requirements for releasing code for rigor and reproducibility in scientific manuscripts.

Hopefully Wolfram will wake up and provide robust support for your python interface, which is critical for participating meaningfully in the rapidly advancing field of ML applications.

POSTED BY: Martin Zand
Posted 7 years ago

For anyone interested in contributing to the project, there's a need for people with non-Mac machines to compile the lib and contribute it to a machine/architecture specific directory so it can be used easily for people without a C compiler.

There's also a need for people to add custom encoder/decoders for efficient data type transfer from Mathematica to convenient python forms. Currently things like PackedArray, RawArray, Image, SparseArray, and HashTable are supported. This uses an encoder on the Mathematica side and a decoder on the python side. Submissions of either are highly appreciated.

All ideas and other contributions are welcome, too.

POSTED BY: b3m2a1 ​ 

Please explain the step "Loading PJLink in Jupyter". Namely:

  1. How do you start Jupyter? (I can start it from anaconda-Navigator.app or from erminal command anaconda-navigator. Is that what I should do?)

  2. Where do I obtain that Demo.ipynb file? (Or is it created using a New command?)

  3. How do I connect a Mathematica kernel to that Jupyter notebook using PJLink?? (If I launch jupyter from anaconda navigator and use the New command, I see no Mathematica choice for the kind, i.e., the kernel.

These are the sort of details whose omission drive potential users to avoidance!

POSTED BY: Murray Eisenberg
Posted 7 years ago

Sorry, I should have been more clear about some things in the post, I think.

As you likely know, Jupyter is a just a front-end for python. I am personally annoyed by/opposed to Anaconda and so only ever start Jupyer from the command line (i.e. with jupyter notebook), but starting Jupyter from Anaconda should be fine. You can also use PJLink without Jupyter, as I generally do—Jupyter is just ersatz Mathematica so I thought it'd be fun to run real and fake Mathematica at the same time.

If you want that demo notebook I can provide it, but all the code is in the post.

PJLink is a dual Mathematica-python package so the way you start the Mathematica kernel is by launching it via python code. See this block in particular:

ker = SubprocessKernel()
def MEval (expr, wait = True, kernel = ker) :
     "" "MEval evaluates a Mathematica expression in the Mathematica kernel
      
      " ""
    kernel.drain() # just to make sure things are clen
    return kernel.evaluate (expr, wait = wait)
ker.start()
ker.start_evaluator()


ker.start() starts the Mathematica kernel and ker.start_evaluator() starts a python-side evaluator that Mathematica can call into. Those two lines are the real magic here. Beyond that I'm just calling into python.

POSTED BY: b3m2a1 ​ 

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! This is wonderful work, thanks for sharing and keep it coming! Note we also placed it in the Add-Ons Group, which is reserved for showcasing of great work in the extended functionality domain.

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