cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
Getting started with Python integration in JMP® 18

JMP 18 has a new way to integrate with Python, providing an independent Python environment designed to be used with JMP. In addition, JMP now has a native Python editor and Python packages specific to JMP. This Python environment in JMP has enhanced connectivity and interaction with JMP, which means using Python with JMP has never been easier.

 

Get familiar with the Python integrated development environment (IDE) in JMP 18 and learn how to

  • Locate the Python IDE.
  • Run a simple example.
  • Install Python packages.
  • Run JSL script from Python.
  • Send a Python variable to JSL.
  • Create a JMP data table from Python.

 

Let's start with how to find the new Python Editor.

 

 

Next, let's next see a simple example in Python and how to reveal the embedded log:

 

Once you've determined which packages are installed, watch how to install a new one and how to upgrade pip.

 

Next, let's learn about the jmp.run_jsl function that encapsulates a JSL script in Python.

 

Now imagine that a CSV data table has been imported with pandas into Python. If we want to send it to the JSL script as a variable, we can use the Python Get function and leverage the JSL functions such as Graph Builder.

 

Finally, let's create a new JMP data table using Python and the "jmp" module. In this example, I am using a csv file and the pandas package but this can be applied to any other type of files, especially those that cannot be opened directly in JMP.

 

 
 

Want more detail? See the Developer Tutorial: New Python Integration and Scripting Capabilities in JMP 18 presented by Paul Nelson, the lead JMP developer for Python integration.

Last Modified: Jul 26, 2024 12:09 AM
Comments
Peter_Hersh
Staff

Great intro @yasmine_hajar 

BHarris
Level VI

Great info!  Looking forward to trying JMP 18 -- we don't have it yet.

 

Just to confirm, data in python has to be pushed into data tables one column at a time?  I would've expected that python could just tell JMP to open a CSV file directly.

gail_massari
Community Manager

Hi @BHarris I am copying @yasmine_hajar and @Paul_Nelson on your question:

Great info!  Looking forward to trying JMP 18 -- we don't have it yet.

 

Just to confirm, data in python has to be pushed into data tables one column at a time?  I would've expected that python could just tell JMP to open a CSV file directly.

 

Paul_Nelson
Staff

The JMP Open, whether it is Open() in JSL or jmp.open( ) when given a CSV file will open it as a data table.  The import jmp package capabilities give the ability to read, modify and create columns from the script.

 

import jmp
# just like you would do in JSL.
dt = jmp.open('Wafer Stacked.csv')
Paul_Nelson
Staff

Paul_Nelson_0-1721321490796.png

 

lala
Level VIII

I want to implement this interaction between JMP and python:

Is this possible in JMP 18、Thanks Experts!:

JMP has opened a JMP file,

dt=Open("$SAMPLE_DATA/Big Class.jmp");

Then call python through JSL, python uses the data of the current table to perform python operations (it is best to call a local py file to calculate the example), python will write the results of the operation to the current table python exit.

JSL then executes further JSL code on the python result of the current file.

HydeMiller
Staff

Hi Lala,

All the items you mentioned are very doable.  In fact, you have a choice, would you rather work with a Python script or a JSL script?

Assuming you are working with a JSL script, things can be sent from JSL to Python using Python Send().  If dt is the JSL pointer to your data table, Python Send(dt) will send it to Python.   This also works for JSL variables that need to be passed to Python.  Python code can be executed inside a JSL script using Python Submit().  Or you might prefer Python Submit File().

Assuming you are working with a Python script, JSL commands can be executed using jmp.run_jsl().  Python can create a new JMP data table using jmp.DataTable().  Python can open an existing table using jmp.open().  Be sure to import the jmp package (import jmp).  

Whichever method you choose, once Python is aware of the JMP data table, Python can direct memory read/write to JMP tables.

Let me know if you have any questions about the details.

Hyde

lala
Level VIII

@HydeMiller 

Thank HydeMiller very much for your timely answers.

 

I have been using JSL with JMP software since the end of 2017 and am now completely dependent on JSL to complete my calculations.
So I want to call python assistance in JSL when needed.
I just want to know if there are more specific examples and code for the steps I mentioned above.
Thanks again to the experts.

hogi
Level XII

Directly including Python code in JSL code is tricky because of

 

python submit("\[here everything gets purple and no syntax highlighting is available]\")

 

 

For small Python scripts, this approach is OK. but debugging is complicated.

 

To jump between JSL and Python code, Workflow Builder should allow Python:
Workflow Builder + Python 

... and there are other options in the pipeline.


At the moment:

open a Python script window, write and debug your code -  maybe play some ping-pong between a Python and a JSL script.
At the end, include your python code via include and reduce the Python part in JSL to something like

 

python submit ("include mylibrary");

//... 

python submit("function(a,b)");

 

 

HydeMiller
Staff

Hi @lala 

 

Hopefully this will get you started.  You will need to install numpy and pandas into your JMP/Python if you have not done so already via Python Install Packages("numpy pandas");

 

Names Default to Here(1);
dt = Open("$Sample_Data\Big Class.jmp");
num_rows = N Rows(dt);
Python Send(dt);
Python Send(num_rows);
Python Submit("\[
import pandas as pd
import numpy as np
import jmp
 
# dt came from JMP
# num_rows came from JMP
 
# build a dataframe from the JMP table
dfx = pd.DataFrame()
for idx in range( len(dt)
    dfx[ dt[idx].name ] = np.array( dt[idx] )
print('********** This is the data frame')
print(dfx)
 
dt.new_column("BMI", jmp.DataType.Numeric)
i = 0
while i < num_rows:
dt["BMI"][i] = dt["weight"][i] / dt["height"][i]
i = i + 1
]\");
lala
Level VIII

@HydeMiller 

Thanks Experts!

I tried the code and it didn't work.

I have installed python as shown in the video on this blog.

import jmp
import jmputils
jmputils.jpip('list')
jmputils.jpip('install','numpy')
jmputils.jpip('install','pandas')
jmputils.jpip('install --upgrade', 'pip setuptools')

But I can see that the JMP/Python directory is not found in my JMP 18 installation directory

 

So I still don't know how it works.

I just wrote the expert to a JSL to run.

 

Still need the help of the experts.

Thank you very much!

 

Paul_Nelson
Staff

What log output did you get from jmputils.jpip() ?

 

The Python site-packages directory is not located within the JMP 18 installation directory because on most systems that's restricted to the installer and administrative users.  Most companies have locked down users so that we no longer have administrative rights on our machines.  If you run

 

import jmp
print(jmp.PY_USER_APPDIR)

It returns the top-level directory that the Python support directories are installed into.

 

Have you restarted JMP since running the package installs?  JMP 18 has an issue that the very first time packages are installed, they may not be recognized until a restart of JMP.  This is due to JMP creating the site-packages directory on demand.  The issue is that when Python is initialized if it doesn't find site-packages/ it won't look for it until after a restart of JMP.  Once it's established, you won't have any issues installing and immediately using packages because site-packages is now present.  This has been fixed in JMP 19 development track, but not been pushed to 18 releases due to the amount of code updated.

lala
Level VIII

@Paul_Nelson 

2024-07-22_22-21-54.png

Thanks!

HydeMiller
Staff

Hi @lala 

 

I'm happy to set up a session with you.  If you are interested, send me some suggested available times hyde.miller@jmp.com.

 

thanks,

Hyde

yasmine_hajar
Staff

Hello all,

Adding here an example where I am transforming a Python Dictionary to a JMP Data table,  with ability to convert the strings to a character column and the numbers to a numeric column.

 

import pandas as pd
import jmp
 
statistical_findings = {
    "ID":["One", "Two"],
    "mean": [75.6, 74.3],
    "median": [76.0, 78],
    "mode": [78, 79],
    "standard_deviation": [8.4, 6.7],
    "variance": [70.56, 67.3],
    "min": [55, 53],
    "max": [92, 87],
    "sample_size": [150, 150]
}
 
jmp_dt=jmp.DataTable("Statistical findings", len(statistical_findings["ID"]))
 
for key in statistical_findings.keys():
if (all(isinstance(id, str) for id in statistical_findings[key])):
jmp_dt.new_column(key, jmp.DataType.Character)
jmp_dt[key]=statistical_findings[key]
else:
jmp_dt.new_column(key, jmp.DataType.Numeric)
jmp_dt[key]=statistical_findings[key]