In the previous installment ( Part 1 ), we introduced the advantage of using JMP's built-in Python integration feature, which allows you to write Python code within JMP, thus complementing the strengths of both tools. For example, you can broaden the range of analysis methods with Python and explore and visualize the results with JMP.
This time, we'll be looking at the relationship between JSL (JMP Scripting Language), JMP's proprietary language, and Python.
JMP's Python integration allows you to call JSL from Python code, and vice versa. In other words, you can write the same process starting from Python or starting from JSL.
P0.png
In this session, using Example 1 from the previous session (data acquisition from the World Bank) as a case study, we will show these two writing methods side by side and organize the characteristics of each and the thinking behind how to use them appropriately.
Executing JSL scripts from a Python environment (a Python-based approach)
In the previous example, we showed Python code to extract economic indicators from the World Bank as a JMP data table. Analysts can then visualize and analyze the extracted data by selecting bubble plots and other options from the JMP menu.
In that case, you might wonder if it's possible to code not only the data extraction but also the creation of JMP analysis reports.
I will illustrate this with the previous example of creating a bubble plot report.
The image below shows the bubble plot report (output) with the options [Trajectory Lines] > [All] and [Labels] > [All] selected from the red triangle button in the upper left. This option displays the country name for each bubble and shows the trajectory of the bubble's movement with lines when the year-by-year trend is animated.
If you select [Save Script] > [Go to Script Window] for this report, a JSL script window will open, where you can view the script that codes the column specifications and options for this report.
P1.png
This script (JSL) can be written in the Python script editor along with the Python code and executed directly. The image below shows the Python script (Example1-1_WorldBank.py) shown previously with the JSL execution code added (Example1-2_WorldBank.py).
jmpパッケージの run_jsl() の中にJSLを記述すると、Python環境の中でJSLスクリプトを実行することができます。
When you use `run_jsl (''' ... ''')` with three single quotes, everything you write between them, including line breaks, will be treated as a string.。
P2.png
The red section in the diagram above is the code that executes the added JSL. Let me briefly explain the JSL part.
最初の Current Data Table() は、現在アクティブになっているデータテーブルを返すコマンドです。この例では、直前に世界銀行の経済指標をJMPのデータテーブルとして取り込んでいるので、そのテーブルを dtという変数に格納しています。また、Set Name() で、データテーブル名を「World Bank Data」に変更しています。
dt = Current Data Table();
dt << Set Name( "World Bank Data" );
The subsequent command changes the display format (Format) of the data table. This is shown as an example of JSL syntax, and the subsequent processing will work without it.
The last part is the bubble plot section. Here, we specify which columns to assign to the current `dt` variable (which we designated earlier) for X, Y, bubble size, etc., and which options to use when executing the plot. The JSL code saved earlier using [Save Script] > [Go to Script Window] will work as is, but the following is a simplified version that includes only the necessary commands.</CODE></FONT></P>
<PRE><CODE class=" language-jsl">dt << Bubble Plot(
X( :GDP_per_capita ),
Y( :Life_expectancy ),
Sizes( :Population ),
Time( :Year ),
Coloring( :Country ),
ID( :Country ),
Label( "All" ),
Trail Lines( "All" ),
);
Running this code will allow you to load data from the World Bank and display a bubble plot report all in one go.
Features for interacting with Python in a JSL environment (writing code starting from JSL)
So far, we've shown that you can execute JSL from a Python environment, but conversely, you can also execute Python code from a JSL environment.
Here is the same code as before (Example1-2_WorldBank.py), rewritten as JSL code (Example1-2_WorldBank.jsl).
JSLの Python Submit() 関数を使うと、引数として渡した文字列の中に書かれたPythonコードを実行できます。今回は、先ほどのPythonスクリプトのうちPythonのコード部分を、Python Submit() の中に入れただけです。
The string you pass to Python's Submit() is "\[ andBy enclosing the text within `]\"`, anything written inside can be treated as text.
P3.png
If you select [Edit] > [Run Script] for this JSL script, you will get the same output as a Python script.
Which approach should I use: writing code starting with Python (.py) or writing code starting with JSL (.jsl)?
The choice depends on which method is the main focus of the processing .
Use a .py file if the main analysis involves Python libraries. Use a .jsl file if you need to create dialogs to select columns or build complex JMP reports.
There are cases where writing code starting with Python (.py) is preferable.
- The analysis will primarily utilize Python libraries such as scikit-learn.
- This is for people who are familiar with Python code.
When is it better to write code starting with JSL (.jsl)?
- The operation of JMP will be central to the overall process.
- Automate and customize the display of JMP reports after analysis.
- Add dialogs for column selection and parameter input to make it a more versatile tool.
In conclusion
In this article, we looked at the relationship between Python and JSL in JMP's Python integration features. The key points are as follows:
- Python環境からは、jmpパッケージの
<EM>run_jsl()</EM> を使ってJSLを実行できる(Pythonを起点とした書き方)
- JSL環境からは、
Python Submit() を使ってPythonコードを実行できる(JSLを起点とした書き方)
- Regardless of which you start with, you can achieve almost the same results; the choice depends on whether the main processing is done on the Python side or the JMP side.
In the next installment ( Part 3 ), we will show an example of how to transform these fixed scripts into versatile tools that anyone can use repeatedly by adding dialogs for column selection and parameter input using JSL.
by Naohiro Masukawa (JMP Japan)
Naohiro Masukawa - JMP User Community