In the previous installment ( Part 2 ), we introduced how JMP's Python integration feature allows you to call JSL from Python code, and vice versa.
Now, let's take another look at the first part of the Python code for importing World Bank data, which we've used as an example so far. The code specifies the target G7 countries, years, and indicators (three of them), and then imports that data.
PythonコードPython code
However, if you want to narrow down the target countries, use different indicators, or change the target year, you will need to rewrite the above code.
While this may not be a major hassle in itself, it becomes inconvenient when you frequently change the conditions or when other JMP users need to use it.
In such cases, it is convenient to provide a dialog box that allows users to select conditions like the following, enabling them to freely specify the conditions they will use for their analysis.
ダイアログdialog
Therefore, this time, I will introduce a method to make a script with fixed conditions more versatile and reusable by writing the column selection and parameter input dialogs in JSL .
Please note that we will not provide a detailed explanation of how to create a dialog box here. The purpose is simply to let you experience firsthand how versatile dialog boxes can be.
Create a dialog box
We will use JSL to create the dialog. Therefore, this is a case where "writing from a JSL-based perspective (.jsl) is preferable ."
Here is an improved version of the JSL code (Example1-2_WorldBank.jsl) introduced last time, which now displays a dialog box at runtime that allows you to do the following:
- Select the target countries from the seven G7 countries (multiple selections are possible using the Ctrl key).
- In addition to the three previous indicators, we've added GDP_growth (GDP growth rate), Inflation (inflation rate), Unemployment (unemployment rate), and Fertility_rate (birth rate), and you can select the target indicator from among them (multiple selections are possible using the Ctrl key).
- Specifying the start and end years
The complete code is attached to this blog post. Here, I will explain the key points regarding the creation of the dialog.
1. Definition of countries and indicators that can be selected in the dialog box.
First, we list the countries and indicators that can be selected. In this example, we have matched the codes used by the World Bank with the names used for display.
// 選択できる国(G7)・指標
countryNames = {"Japan", "United States", "Canada", "United Kingdom", "Germany", "France", "Italy"};
countryCodes = {"JP", "US", "CA", "GB", "DE", "FR", "IT"};
/* 選択できる指標: 人口,一人当たりGDP,経済成長率,インフレ率, 失業率,出生率,平均寿命 */
indNames = {"Population", "GDP_per_capita", "GDP_growth", "Inflation",
"Unemployment", "Fertility_rate", "Life_expectancy"};
indCodes = {"SP.POP.TOTL", "NY.GDP.PCAP.CD", "NY.GDP.MKTP.KD.ZG", "FP.CPI.TOTL.ZG",
"SL.UEM.TOTL.ZS", "SP.DYN.TFRT.IN", "SP.DYN.LE00.IN"};
2. Dialog section
We will create a dialog using the New Window() function . An example of how to write the code is provided in the JMP scripting guide.
The dialog box is arranged using V List Box (vertical), H List Box (horizontal), and Panel Box (heading frame), and within these, you place a List Box for selecting from a list and a Number Edit Box for entering numerical values.
// ダイアログの起動
selCountry = {};
selInd = {};
startY = .;
endY = .;
nw = New Window( "Get World Bank Data",
<
<MODAL></MODAL>
<BR /> <P><FONT size="4" color="#0000FF">3. Creating variables to pass to Python</FONT></P> <P><FONT size="4">The selected country names and indicator names are converted into codes used by the World Bank and listed as an associative array. <STRONG>The key point is the subsequent Python Send(), which passes the JSL variables as Python variables with the same names.</STRONG> This allows the Python code in the subsequent Python Submit() to use `countries` and other variables directly.</FONT></P> <CODE class=" language-jsl">// Python に渡す変数の作成
countries = {};
For( i = 1, i <= N Items( selCountry ), i++,
Insert Into( countries, countryMap[selCountry[i]] )
);
indicators = {};
For( i = 1, i <= N Items( selInd ), i++,
Insert Into( indicators, indCodeMap[selInd[i]] )
);
colnames = selInd;
// Show( countries, indicators, colnames, startY, endY );
Python Send( countries );
Python Send( indicators );
Python Send( colnames );
Python Send( startY );
Python Send( endY ); </CODE></FONT></P> <P><FONT size="4">Using the values of the variables passed to Python, the pandas_datareader package will output a data table for the specified country, indicator, and year.</FONT></P> <P><FONT size="2">Note: This code prioritizes conciseness, so it does not include much code for checking input values or error handling.</FONT></P> <BR /> <P><FONT size="5" color="#FF6600">Visualize and analyze data obtained through Python integration using JMP.</FONT></P> <P><FONT size="4">When you run the JSL script, the dialog you created will appear, allowing you to specify the country, indicator, and year to create the data.</FONT></P> <P><FONT size="4">To compare the trends in GDP growth rates, inflation rates, and unemployment rates in Japan, the United States, Germany, and France before and after the COVID-19 pandemic, we will create data covering the period from 2017 to 2024.</FONT></P> <P><FONT size="4">If you can obtain the data as a JMP data table, you can display a graph below the column names by clicking the histogram icon (header graph) in the data table. Selecting a relevant part of the graph will select the corresponding data, allowing you to explore the data to some extent within the data table.</FONT></P> <P><FONT size="4"><SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ダイアログによる国と指標の選択" style="width: 999px;"><span class="lia-inline-image-display-wrapper" image-alt="ダイアログによる国と指標の選択" style="width: 999px;"><img src="https://us.v-cdn.net/6038925/uploads/images/112217i69CB0159845A07E6/P4.png" role="button" title="P4.png" alt="ダイアログによる国と指標の選択" /><span class="lia-inline-image-caption" onclick="event.preventDefault();">ダイアログによる国と指標の選択</span></span><SPAN class="lia-inline-image-caption" onclick="event.preventDefault();">Dialogue-based selection of countries and indicators</SPAN></SPAN></FONT></P> <P><FONT size="4">This data can also be used to analyze year-to-year trends with animation using a bubble plot, as introduced in the first installment. Furthermore, using the [Graph Builder], you can create line graphs and bar graphs showing year-to-year trends for indicators, as shown below.</FONT></P> <P><FONT size="4"><SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="可視化" style="width: 999px;"><span class="lia-inline-image-display-wrapper" image-alt="可視化" style="width: 999px;"><img src="https://us.v-cdn.net/6038925/uploads/images/112018iC6D52C5CF3BFD2FC/fig4.png" role="button" title="fig4.png" alt="可視化" /><span class="lia-inline-image-caption" onclick="event.preventDefault();">可視化</span></span><SPAN class="lia-inline-image-caption" onclick="event.preventDefault();">Visualization</SPAN></SPAN></FONT></P> <P><FONT size="4">Furthermore, using [time series forecasting], you can fit a smoothing model to obtain future predicted values and forecast intervals.</FONT></P> <P><FONT size="4"><SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="時系列モデル" style="width: 627px;"><span class="lia-inline-image-display-wrapper" image-alt="時系列モデル" style="width: 627px;"><img src="https://us.v-cdn.net/6038925/uploads/images/112218i3DBB79F853ECEB1B/P6.png" role="button" title="P6.png" alt="時系列モデル" /><span class="lia-inline-image-caption" onclick="event.preventDefault();">時系列モデル</span></span><SPAN class="lia-inline-image-caption" onclick="event.preventDefault();">Time series model</SPAN></SPAN></FONT></P> <BR /> <P><FONT size="5" color="#FF6600">In conclusion</FONT></P> <P>In this article, I introduced a method for creating a "general-purpose tool" by combining data retrieval code written in Python with a dialog box created in JSL.</P> <P><FONT size="4">The main points are the following three:</FONT></P>
<UL>
<LI><FONT size="4"><STRONG>Dialogs are created using JSL:</STRONG> Therefore, even if the core processing is in Python, it's easier to handle if the starting script is in .jsl format. This is one of the specific points of contention in the decision of "write in .py or .jsl" that we discussed in Part 2.</FONT></LI>
</UL>
<UL>
<LI><FONT size="4"><STRONG>The received values are passed to Python using Python's Send() method:</STRONG> Since the JSL variable names become the same variable names in Python, you hardly need to rewrite the Python code itself.</FONT></LI>
<LI><FONT size="4"><STRONG>After importing the data, you can explore it using the usual JMP data tables:</STRONG> you can use JMP's analysis functions as is, such as graph builders and time series forecasting.</FONT></LI>
</UL> <P><FONT size="4">By using Python to acquire and process data, and then using JMP for visualization and analysis, and delegating the specification of the entry conditions to a dialog box, the conditions can be provided externally without changing the main script. This increases the versatility of the script through the use of dialog boxes.</FONT></P> <P><FONT size="4">In the next installment ( <A href="https://community.jmp.com/t5/Naohiro-Masukawa/%E7%B5%90%E5%B1%80-JMP%E3%81%AE-Python%E9%80%A3%E6%90%BA-%E3%81%A7%E4%BD%95%E3%81%8C%E3%81%A7%E3%81%8D%E3%82%8B%E3%81%AE%E3%81%8B-%E7%AC%AC4%E5%9B%9E-JMP%E3%81%AB%E3%81%AA%E3%81%84%E5%88%86%E6%9E%90%E6%89%8B%E6%B3%95%E3%82%92-JMP%E3%81%AE%E6%93%8D%E4%BD%9C%E6%84%9F%E3%81%A7%E4%BD%BF%E3%81%86-%E5%85%A84%E5%9B%9E%E3%81%AE%E3%81%BE%E3%81%A8%E3%82%81%E5%8B%95%E7%94%BB%E3%81%82%E3%82%8A/ba-p/963459" target="_blank">Part 4</A> ), as a further step towards generalization, we will introduce an example of using Python packages to execute functions not included in JMP, and specifying the variables (columns) to be analyzed using a dialog box similar to the one we created this time.</FONT></P> <BR /> <P><FONT size="4">by Naohiro Masukawa (JMP Japan)</FONT></P> <P><FONT size="4"><U><A href="https://community.jmp.com/t5/Naohiro-Masukawa/bg-p/nmasukawa-blog" target="_blank">Naohiro Masukawa - JMP User Community</A></U></FONT></P>
</LINGO-BODY>
<CODE class=" language-jsl">
<LINGO-TEASER id="lingo-teaser-962736" slang="ja-JP" mode="UPDATE">
<P>This article briefly introduces the role of a dialog box (an operation screen where users select and input conditions such as target countries, indicators, and time periods). It then explains how to create a general-purpose data acquisition tool that can flexibly adapt to changes in conditions by creating a dialog box using JMP's JSL and integrating it with Python. The advantage of using a dialog box is that usage conditions can be changed without rewriting the script, making repeated use and sharing with other users much easier.</P>
<BR />
</LINGO-TEASER>