Change node settings in supernodes with SPSS Modeler Python scripts

Supernodes and python scripts

I often use supernodes to organize (make it easier to see) long streams in SPSS Modeler, but I don't know how to handle nodes in supernodes when changing node settings in Python scripts. Did. (If you expand the supernode, it will be solved, but at this time there are many supernodes, and I wanted to solve it with a script if possible.) I will leave a way to realize it as a memorandum.

First of all, the manual

In conclusion, the method is well documented and found quickly.

IBM SPSS Modeler 18.2 Python Scripting and Automation Guide (http://public.dhe.ibm.com/software/analytics/spss/documentation/modeler/18.2/en/ModelerScriptingAutomation.pdf)

P.383 Excerpt ** Encapsulation node property settings ** You can set the properties of a particular node encapsulated within a supernode by accessing the child diagram within the supernode. For example, suppose you have an input supernode with a variable length file encapsulated to read data. You can pass the name of the read file (specified using the full_filename property) by accessing the child diagram and searching for related nodes as follows:

childDiagram = source_supernode.getChildDiagram() varfilenode = childDiagram.findByType("variablefile", None) varfilenode.setPropertyValue("full_filename", "c:/mydata.txt")


## Practical edition
 If you can understand it easily by looking at the above manual, you don't have to read it anymore.

### Example stream
 (1) There is such a stream that RFM aggregates from purchase history data
 <img width="300" alt="WS0057.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/85ea0092-1ffd-9966-6a51-efdfaab41b8e.jpeg ">
 ② Suppose that the contents of the super node are like this.
 <img width="900" alt="WS0058.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/0cf2ef31-2c6b-8da5-9668-c8083723020c.jpeg ">

 The contents of the field creation node that is "RECENCY" in the super node is now like this.
 <img width="600" alt="WS0061.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/b78affdd-74af-886d-6d15-906c24352bf3.jpeg ">


 What we are doing is calculating how many days ago the latest purchase date is based on 2020/10/1.
 (SDATE is the purchase date. Think of SDATE_Max as the latest purchase date for each customer in the pre-aggregate node.)

 Here, suppose that you want to change the base date of 2020/10/1 according to the execution timing of the stream, for example, you want to aggregate based on the previous Sunday no matter when you execute it.

 In that case, you can change the datetime_date (2020, 10, 1) part in the Python script to the appropriate date you want to use as the reference, but since the nodes in the supernode cannot be handled directly by findByXXXX (), the previous manual It will take some effort as seen in.

 Below is an example of a Python script.

import datetime stream = modeler.script.stream()

#Base date setting RefDate = datetime.date(2020, 10, 10)

#Identify supernodes in the stream rfm_process_super_node = stream.findByType("process_super", u"RFM aggregation") #Instantiate a diagram (stream?) Expanded inside a supernode rfm_child_diagram = rfm_process_super_node.getChildDiagram() #Identify the field creation node in the supernode rfm_derive_node = rfm_child_diagram.findByType("derive", u"RECENCY") #Change base date for specified field creation node rfm_derive_node.setPropertyValue("formula_expr", u"date_days_difference(SDATE_Max,datetime_date(" + str(RefDate.year) + "," + str(RefDate.month) + "," + str(RefDate.day) + ")")


 It will be a little easier to understand if you understand that "stream" is the stream of ① and "rfm_child_diagram" is the stream in the supernode of ②.

 If you execute this script in the stream (Tools ⇒ Stream Properties ⇒ Execute), the base date will change to 2020/10/10. This time, I have specified the base date for simplicity, but please think about the logic of what to do with the base date according to your requirements.

 Below is the setting of the node that was changed by running the script. It went well.
 (I never rewrote it by hand!)

 <img width="600" alt="WS0062.JPG" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/361272/1aef661a-afdc-cf5d-f68c-8b9f7b988528.jpeg ">


 * Please refrain from rushing into such a short stream as "Isn't it necessary to make it a super node?"



Recommended Posts

Change node settings in supernodes with SPSS Modeler Python scripts
Rewrite field order nodes in SPSS Modeler with Python.
Rewrite the record addition node of SPSS Modeler with Python.
Rewrite the sampling node of SPSS Modeler with Python (2): Layered sampling, cluster sampling
Using Python with SPSS Modeler extension node (2) Model creation using Spark MLlib
Rewrite SPSS Modeler reconfigure node in Python. Aggregation by purchased product category
Settings for getting started with MongoDB in python
Change IP settings to ACL of conoha with python
VS Code settings for developing in Python with completion
Working with LibreOffice in Python
Scraping with chromedriver in python
Dynamically import scripts in Python
Working with sounds in Python
Scraping with Selenium in Python
Using Python with SPSS Modeler extension nodes ① Setup and visualization
[Python] Change dtype with pandas
Change retry settings with boto3
Scraping with Tor in Python
Combined with permutations in Python
Try Juniper JUNOS PyEz (python library) Memo 3 ~ Change settings with PyEz ~
How to pass arguments to a Python script in SPSS Modeler Batch
Testing with random numbers in Python
Scraping with Node, Ruby and Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Utilize Python custom scripts with StackStorm
Use Python in pyenv with NeoVim
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Working with DICOM images in Python
Rewrite the field creation node of SPSS Modeler with Python. Feature extraction from time series sensor data
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Competitive programming with python Local environment settings
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Device monitoring with On-box Python in IOS-XE
Change batch settings with Mailman's withlist command
Try working with binary data in Python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Manipulate DynamoDB data with Lambda (Node & Python)
Template for writing batch scripts in python
Dealing with "years and months" in Python
Process multiple lists with for in Python
Replace non-ASCII with regular expressions in Python
Connect with mysql.connector with ssh tunnel in Python 3.7
One liner webServer (with CGI) in python