5. Using NetMiner Script
5. Using NetMiner Script
1
1. Introduction
2
Using NetMiner Script
The information in this publication is provided for information only and is subject to change without
notice. Cyram Inc. assume no responsibility or liability for any loss or damage that may arise from the
use of any information in this publication. The software described in this book is furnished under
license and may only be used or copied in accordance with the terms of that license.
NetMiner is a registered trademark of Cyram Inc. CYRAM and CYRAM logo are registered
trademarks of Cyram Inc.
Cyram Inc.
#904, U-Space 2B, 670, Daewangpangyo-ro, Bundang-gu, Seongnam-si, Gyeonggi-do, 13494,
South Korea
Tel: +82-31-739-8352
Fax: +82-31-739-8354
In order to reference NetMiner 4, please employ the following bold-faced reference, which is also
found at NetMiner Menu: “Help >> About NetMiner 4”.
Technical Support
The NetMiner Help file will guide you to use NetMiner 4. Simply press F1 function key or use
3
1. Introduction
Cyram Inc.
#904, U-Space 2B, 670, Daewangpangyo-ro, Bundang-gu, Seongnam-si, Gyeonggi-do, 13494,
South Korea
Tel: +82-31-739-8352
Fax: +82-31-739-8354
Email: netminer@cyram.com Web: http://www.netminer.com/
4
Using NetMiner Script
Contents
1. INTRODUCTION ....................................................................................................... 9
5
1. Introduction
6
Using NetMiner Script
7
1. Introduction
8
Using NetMiner Script
1. Introduction
1.1 What is NetMiner Script?
Based on the Python syntax, NetMiner Script provides an environment for performing
analysis and visualization with respect to various aspects of NetMiner. Execution in
NetMiner Script is exactly the same as execution through the GUI not only in terms of
results but in terms of process as well. Therefore by looking at a script the user can easily
understand what specific task is to be performed in the overall process. NetMiner Script
supports the Python language constructs and modules by default and provides various
functions for network analysis and visualization. With NetMiner Script you can automate
analysis/visualization processes that have to be performed repeatedly as well as perform
condition-based analysis. In addition, external modules written purely in Python can be used
for application in a broader field. The Script Workbench is GUI program for more efficient
use of NetMiner scripts. The script generator of the Script Workbench allows the use of user
interfaces of control panels of an existing NetMiner session so that complex scripts of
analysis and visualization can be more easily written. The Script Workbench provides a
guide box for each NetMiner function as well as a list of functions.
1) Most features of NetMiner can be performed using a script rather than having to click
with mouse so that complicated series of commands can be stored in scripts and executed
repeatedly without additional work.
2) Analysis flows, such as loops and conditional branching can be controlled by the user in a
flexible way.
3) Various existing math and statistics modules written in Python can be imported without
modification for use, and ordinary data structures provided by Python can be defined.
9
1. Introduction
4) Simple algorithms can be written by the user in order to be used in combination with
NetMiner features. In addition, user-written files can be added to the NetMiner menu to be
used like internal processes which can be shared with other NetMiner users as well.
1) Python-based scripting
Most analysis software packages with scripting feature use their own scripting language.
This requires the user to learn the language, which limits the user base and consequently it’s
not easy to find related material either.
NetMiner Script uses Python, a programming language distributed free of charge and
widely used all over the world. Non-computer majors often get cold feet when they hear the
word programming language, but Python is especially so easy that even complete beginners
who have little knowledge of programming can learn and use it without difficulties,
provided they put in some effort. Also, Python has a wide user base, which means there are
lots of easily-obtainable materials available on the Web for learning Python. To use analysis
algorithms involving math, statistics or data mining for example, you go to a place on the
Web where Python codes written by other people are shared and then, you can download
and use the pertinent source codes. This is especially helpful for Python beginners.
Therefore, learning NetMiner Script is not simply learning another script language – it is
also a gateway to help you to enter the vast world of Python.
2) WYSIWYG scripting
NetMiner is a GUI program and NetMiner Script is a GUI-based script, which means all
tasks executed through the GUI can be controlled by the use of scripts. Although NetMiner
is not the only analysis software that uses both GUI and script, it does differentiate itself
from others in the following respects: Generally, in the case of analysis software that uses
both of the interfaces, although the results of execution in GUI mode and scripting mode are
the same, their processes are not exactly the same. In most of the analysis programs, scripts
control data and processes in a way unrelated to the GUI. In contrast, the way of writing
10
Using NetMiner Script
NetMiner scripts, is the same. Therefore, while writing a NetMiner Script, the user can see
in their mind’s eye which task is corresponding to the execution in GUI. To make it more
convenient in generating scripts, NetMiner enables users to utilize the script generator in the
Script Workbench. By using it, scripts can be automatically generated according to items
selected through the GUI, which helps to avoid the hassle of manual coding in each process.
Because of this, NetMiner Script can be called a WYSIWYG (What You See Is What You
Get) way of scripting.
1.2 Installation
Refer to the installation guide included in the CD for the installation. The installation
guide can also be downloaded on the Web site, at the following address:
http://www.netminer.com>>Download>>Documents>>Installation_Guide.pdf
To use Script Workbench, NetMiner 4.0 or above is required. Therefore you have to use
NetMiner 4.0 in order to use Script Workbench. If you purchased NetMiner 3, you can get a
special discount when purchasing NetMiner 4. Please visit the Web site for details.
If you registered NetMiner 4 with a license key that includes the scripting package in the
initial installation process of NetMiner 4, you can immediately begin scripting. If you didn’t
register it with a license key that includes the scripting package, however, you can click
‘Help >> Register…’ in the main menu of NetMiner 4 and register it with a new license key
that allows for scripting. Once ‘Tools >> Script Workbench’ is activated, you are ready to
go to begin using NetMiner Script.
11
1. Introduction
Step 1: Double click on the NetMiner 4 icon to run NetMiner 4, or execute the file
‘NetMiner 4.exe’ located in the NetMiner 4 installation folder.
Step 2: Click on ‘Tools >> Script Workbench’ in the main menu bar of NetMiner 4.
12
Using NetMiner Script
1) Main menu
The main menu has various menus needed for writing scripts.
2) Tool Bar
‘New’ ‘Run’
13
2. NetMiner scripting environment: Script Workbench
3) Edit window
The edit window is the place where you write a script. It provides convenient features
such as autocompletion, line indication, and text color differentiation which enable
programming more efficiently.
4) Log window
The log window shows the results of running a script performed by ‘print’ lines. When an
error occurs, it shows the cause and location where error occurs.
5) Variable window
The variable window shows declared variables in the script analysis process, user-defined
functions and contents.
The menu of Script Workbench is composed of File, Edit, View, Script, and Plug-in.
1) File
Open Open a script file (.py) and show the contents in the edit window. Ctrl + O
Save Save the script being edited in the edit window. Ctrl + S
Save As Save the script being edited in the edit window as a new file.
Close Tab Close the currently active tab in the edit window. Ctrl + F4
2) Edit
14
Using NetMiner Script
Select All Select the entire content of the script being edited. Ctrl + A
Toggle Comment Treat the selected area as a comment. If the selected area is a Ctrl + /
comment, cancel that status.
Shift Right Shift the selected area to the right one step. Tab
Shift Left Shift the selected area to the left one step. Shift + Tab
Find/Replace Find a specific word and replace it with another word. Ctrl + F
3) View
4) Script
Run Selected Lines Run the line where the cursor currently is or run the Ctrl + L
selected area.
15
2. NetMiner scripting environment: Script Workbench
Context Show function arguments. It works only when the cursor Ctrl + Shift +
Information is inside the parenthesis. Space
2.2.1 Edit
1) Autocompletion
NetMiner Script not only supports Python but provides functions unique to NetMiner
Script. Script Workbench provide autocompletion for these functions. In the menu, select
‘Script >> Content Assist’ or press the shortcut key Ctrl+Space to bring up a list of classes
and functions for autocompletion.
NetMiner Script provides classes and functions unique to NetMiner as well as classes and
sub functions for processing matrix data, which can be used by entering ‘.’ after the class or
function name. Autocompletion feature is provided as well for these, as shown below.
16
Using NetMiner Script
In addition, if the cursor is held over a function unique to NetMiner Script, the description
of the function is shown, in addition to its arguments.
Finally, when loading a file or exporting it, the file path can be auto-completed through
the following method.
17
2. NetMiner scripting environment: Script Workbench
Step 1: Go to the folder where the file is, copy the file or select the file and press Ctrl+C.
Step 2: Place the cursor where you want write the path in the Script Workbench edit
window. Click ‘Edit >> Paste’ or press Ctrl+V to autocomplete the path as shown below.
The color of the content of a script is determined according to the following rules.
Color Item
Green Strings
Blue Control statements (for, if, elif, else, while, continue, break), logical operators (and, or),
logic values (None, True, False), user-defined functions (def)
Brown Numbers
Gray Comments
18
Using NetMiner Script
Script Workbench provides a feature whether the line numbers can be shown or hidden. If
shown, they are shown in the leftmost side of the edit window, as shown below. The feature
can be turned on and off by clicking ‘View >> Line Number’ or pressing the shortcut key
Ctrl+I.
This feature shows where the cursor is, in terms of line and column. It is shown in the
lower right corner of Script Workbench.
19
2. NetMiner scripting environment: Script Workbench
5) Right click
If you right click with the mouse in the edit window or do it with a portion of the script
selected, the following feature can be used in more convenient way.
6) Font
You can change the font used in Script Workbench by clicking ‘Edit >> Editor Font’.
20
Using NetMiner Script
The script generator supports writing of scripts for analysis modules. All modules are
supported via the main menu of NetMiner, including Transform, Analyze, Statistics,
Visualize, and Chart.
Select an analysis module and then options in the user interface that is the same as the
control panel to generate automatically the corresponding script. The script generator can be
run by clicking 'Script >> Script Generator', which can be run while a project is open.
Under the script generator, analysis session scripts can be very easily written by using the
same option of UI as the one in NetMiner.
2.2.3 Running
21
2. NetMiner scripting environment: Script Workbench
In Script Workbench, scripts that use Python language features and NetMiner script
functions can be run immediately without a special compile command. The results of
execution are displayed in the log window and the NetMiner process results panel. .
After writing a script, click ‘Script >> Run’ or press the shortcut key Ctrl+R to run it.
In addition, you can execute only selected lines of a script by selecting an area of the
script and clicking on ‘Script >> Run Selected Lines’ or by pressing the shortcut key Ctrl+L.
In addition, you can right click on the selected area to bring up a pop-up menu to do this.
2.2.4 View
The log window shows the results of running a script while the Variable window shows
variable information.
22
Using NetMiner Script
1) Logs window
As shown below, results of various computations of data are shown by using ‘print’
statements.
When errors occurred after running the script, the cause and location where they occurred
are shown as below.
23
2. NetMiner scripting environment: Script Workbench
If you want to delete the contents in the log window, click on the ‘Clear Screen’ button at
the bottom of the log window.
2) Variables window
Information of user-defined variables and functions is shown after executing the script, in
terms of Name, Category, Type, and Value/Info, as shown below.
For Value/Info, if the data type is not a single value but a row or list data, it is shown in
the form of buttons in a table. The entities in the table are identified by their row and column.
When a button is clicked, data values are shown in a new window, as is done below.
24
Using NetMiner Script
If you want to delete all variable information created so far, click on the ‘Delete All
Variables’ button located at the bottom of the Variable window.
(1) integer
-Variable window
-Value/Info
The value is displayed.
(2) float
-Variable window
-Value/Info
The value is displayed.
25
2. NetMiner scripting environment: Script Workbench
(3) complex
-Variable window
-Value/Info
The value is displayed.
(4) long
-Variable window
-Value/Info
The value is displayed.
(5) boolean
-Variable window
-Value/Info
The value is displayed.
(6) string
-Variable window
-Value/Info
The value is displayed.
26
Using NetMiner Script
(7) list
-Variable window
-Value/Info (click)
(8) tuple
-Variable window
-Value/Info (click)
(9) dictionary
-Variable window
-Value/Info (click)
27
2. NetMiner scripting environment: Script Workbench
(10) set
-Variable window
-Value/Info (click)
(11) file
-Variable window
-Value/Info (click)
[Show the file path and whether it’s a file for reading or for writing]
(12) function
-Variable window
28
Using NetMiner Script
-Value/Info (click)
-Value/Info (click)
(14) module
-Variable window
29
2. NetMiner scripting environment: Script Workbench
-Value/Info (click)
-Value/Info (click)
30
Using NetMiner Script
(16) stringTable
-Variable window
-Value/Info (click)
31
2. NetMiner scripting environment: Script Workbench
(17) graphNode
-Variable window
-Value/Info (click)
32
Using NetMiner Script
-Value/Info (click)
-Value/Info (click)
[File type, path, and NetMiner data name and data type to import are shown]
[File type, path, and NetMiner data and data type to export are shown]
(20) matrix
-Variable window
33
2. NetMiner scripting environment: Script Workbench
-Value/Info (click)
(21) matrixIterator
-Variable window
-Value/Info (click)
[Data are shown in terms of row index, column index, and value]
(22) customDialog
-Variable window
-Value/Info (click)
34
Using NetMiner Script
(23) objectArray
-Variable window
-Value/Info (click)
[Most are in the form of object array when NetMiner data are loaded]
35
3. Using NetMiner Script Function
There are main function classes in which NetMiner script functions are put in. Specifically,
by a tree structure they are put into sub classes according to their type. Therefore, to use a
function, located in a leaf node, begin with the main function class and work your way down
through sub classes.
In the figure shown below, denotes a class while denotes a function. In NetMiner
Basically, the structure of NetMiner script functions is as shown above. To use the
[getHtmlData] function in the above figure, you can write the script as shown below
according to the hierarchical structure.
session1 = _NM.Session.createSession('test')
mainReport = session1.getMainReport()
htmlData = mainReport.getHtmlData()
※ Warning: As seen here, keep in mind that data are returned by functions. However, it is
not possible to get a class itself as a return value, such as in ‘nm = _NM’ or ‘session1 =
_NM.Session’.
36
Using NetMiner Script
Tree structures for all NetMiner Script functions are shown in Appendix A.
The basic data classes are prefixed with an underscore (‘_’), denoting that they’re
the root classes.
There are three basic data classes, namely _NM, _MAT, _SYS.
_NM: Class that contains functions working with unique NetMiner data
37
3. Using NetMiner Script Function
Ex ) _ NM.Project.saveProject()
_NM.Dataset.getMainAttributeNames()
matrix.getRowSize()
※ Here, matrix is a data class created by _MAT.Create or _MAT.FromNM.
(2) _BasicDataClass.SubClass.FunctionName(arguments)
38
Using NetMiner Script
or
DataClass.FunctionName(arguments)
Ex ) NM.saveAsProject(r’c:\test.nmf’)
NM.deleteSession(‘session1’)
Ex ) workFileNames = NM.getCurrentWorkfileName()
session = NM.getFocusedSession()
In NetMiner Script, you call functions contained by the basic data classes (_NM, _MAT,
_SYS) as shown above.
In addition, functions that have arguments must be provided with arguments, and when a
function has a return value, it must be put into a variable declared by the Python variable
naming convention.
39
3. Using NetMiner Script Function
For functions that require arguments, arguments of appropriate type must be passed in,
inside the parenthesis.
_NM.Project.saveAsProject(r’c:\test.nmf’) _NM.Session.deleteSession(‘session1’)
This type of function yields different results according to the arguments passed in.
Most of the time, datatypes of arguments to NetMiner functions are string, integer, float, or
boolean. When passing in a string that contains a file path, etc., you have to be careful to
prefix the text with ‘r’. See as follows.
r’c:\example\test.dat’
When passing in multiple arguments, they have to be separated by a comma (,) and the
arguments have to be passed in the right order.
Pass in a value for the noteSetName
_NM.Dataset.getSubNodeAttribute(‘Clubs’, ‘Name’) argument first and then a value for the
attName argument second.
If you don’t provide equivalent values for all arguments to function, an error will occur.
Arguments to a function that are denoted by square brackets can be omitted. No problems
will occur in executing the function even if they’re omitted. This is because the arguments
denoted by square brackets have default values associated with them. When you don’t
specify a value, the default value will be used.
40
Using NetMiner Script
Ex ) _ NM.Project.saveAsProject(r’c:\test.nmf’)
_NM.Session.deleteSession(‘session1’)
_NM.Project.saveProject()
Most functions without return values usually perform some NetMiner operation.
For example, the function _NM.Project.saveProject( ) saves the current project. Because of
that, there is no need to get a return value as in ‘project = _NM.Project.saveProject( )’.
While doing ‘project = _NM.Project.saveProject( )’ will run, the variable project will get
‘NoneType’.
Functions that requires a return value are described at the basic form of variable name =
function() or variable name = function(arguments). Notice that, no errors occur even if
you don’t input a variable for getting the return value. In this case you only run a useless
41
3. Using NetMiner Script Function
In NetMiner Script, there are three different datatypes for return values: Python datatypes,
NetMiner datatypes, and matrix datatypes.
For details on the datatypes, refer to Appendix B. In this section, we’ll briefly examine some
of the major types and forms of expression so as to get the return values.
1) Python datatypes
This is the datatypes in a general form of list, integer, decimal and Boolean as a return
value of NetMiner Script functions. There datatype can be used as argument or used in
combination with external Python modules.
2) NetMiner datatypes
42
Using NetMiner Script
NetMiner datatypes are the one unique to NetMiner and separate from those of Python, and
accordingly values returned can have functions as well. There are seven main NetMiner
datatypes: Session for analysis; Report for dealing with analysis results; StringTable for
managing string tables; Import/Export for file input and output; GraphNode for managing
nodes in a graph; QuerySet for queries; CustomDialog for creating dialog boxes.
Datatype Displayed as Example
session = _NM.Session.createSession(Analyze >> Centralty
Session session >> Degree)
session = _NM.Session.openSession("Degree")
mainreport mainreport = session.getMainReport("[R] Main")
tablereport tablereport = session.getTableReport("[T] table")
Report
mapreport mapreport = session.getMapReport("[M] map")
chartreport chartreport = session.getChartReport("[C] chart")
String Table stringtable stringtable = tablereport.getStringTable( )
Graph Node graphnode graphnode = graphnode[ ][index]
Query Set queryset queryset = _NM.Query.createQuerySet('querySetName')
importer importer = _NM.Import.createTextImporter(r'c:\a.txt')
Import/Export
exporter exporter = _NM.Export.createTextExporter(r'c:\a.txt')
Custom Dialog customdialog customdialog = _SYS.Dialog.createCustomDialog( )
3) Matrix datatypes
This is a datatype that can be created manually in Matrix type or the one to be created after
NetMiner data is taken to. Matrix iterator is a type of list that contains not 0s but index or
element values.
Datatype Displayed as Output type
1.0 1.0 1.0
Matrix matrix 2.0 1.0 0.0
0.0 3.0 2.0
(0,0) - 1.000000
(1,1) - 1.000000
Matrix Iterator matrixiterator
(1,2) - 1.000000
(3,3) - 1.000000
43
4. Using NetMiner Script
- Creating a project -
This section shows how to create a new project. It performs the same function as the one
selecting Blank Project in ‘File >> New >> Project’ of the NetMiner main menu.
_NM.Project.newProject();
UNMS_1.py
Step 2: Click on ‘Script >> Run’ or use the shortcut key (Ctrl+R) to run the script. If there is
a project already open, it will be deleted without warning and a blank project will be created
as follows.
- Opening a project
44
Using NetMiner Script
This section shows how to open a project file (*.nmf). It performs the same function as
‘File >> Open’ of the NetMiner main menu.
Step 1: To load a NetMiner file (.nmf), write the following script. In the example the file
“01.Org_Net_Tiny1.nmf” was used.
filePath = r'%UserProfile%SampleData\Cyram
Dataset\01.Org_Net_Tiny1\01.Org_Net_Tiny1.nmf'; #(1)
_NM.Project.openProject(filePath); #(2)
UNMS_2.py
(1): Enter a string for the path to the file to open. The ‘r’ in front of the character string is
used to treat characters such as ‘\’ literally.
(2): A function to open the project. This function gets an argument in a string that is
developed in(1).
Step 2: Click ‘Script >> Run’ or use the shortcut key (Ctrl+R) to run the script. Confirm that
the file '01.Org_Net_Tiny1.nmf' opens up.
※ To use such features as the script generator of Script Workbench, a project must be open
beforehand.
45
4. Using NetMiner Script
※ If there is already a project open when opening a new project by Script, Notice that you
won’t be asked whether to save the existing project before opening the new project.
In Script Workbench, you can make a script by using the Script Generator to create a new
project or to open an existing project. This is especially useful when the path is complicated.
Step 2: Select a process under File in the script generator. New Project will generate a script
that creates a new project while Open Project will generate a script that opens an existing
one.
- New Project
Select File > New Project in the script generator. Opening a new project is unrelated to
creating a session, so Session Varible Name input window will be deactivated. Press OK to
generate the script, as follows.
46
Using NetMiner Script
- Open Project
In the script generator, select File > Open Project. Opening an existing project is also
unrelated to creating a session, so Session Variable Name input window will be deactivated.
Press OK to generate the script as follows.
In Open Project, the project file (*.nmf) to open is selected, so the Option window will be
open as shown above. You can select the file by clicking Browse.
Check that the path to the file is correct and press the OK button in the script generator
window. A script for opening a specific .nmf file will be automatically generated as follows.
47
4. Using NetMiner Script
You can import external data by using NetMiner Script. It performs the same function as
is the case of ‘File >> Import’ in NetMiner. File types that can be imported are text files
(*.txt, *.csv) and Excel files (*.xls).
In this example, sample data of ‘01.Org_Net_Tiny1’ was used. If you installed NetMiner
4 with the default settings, you can find the sample data in the folder
'C:\Users\(Username)\NetMiner\SampleData'.
The process to write a script for importing external data is segmented roughly into three
parts.
To import data using a script, create an object for it. The created object includes functions
for setting options.
To create an object for importing text data (*.txt, *.csv), write the following script.
(1): Enter the file path in string variable. Notice that ‘r’ is used to treat special characters
such as ‘\’ literally.
(2): Create an object for importing text data and assign it to ‘importer’. The arguments to the
function ‘createTextImporter’ are file path, line separator, and text qualifier. The file path is
where the data to import is located, and is a mandatory argument. The line separator is used
to specify a character by which to identify the end of each line is identified. When loading
a .csv file, comma (‘,’) is the default and when loading a .txt file, tab (‘\t’) is the default. The
text qualifier shows text data inside a specified character. By default (“ “) is used. The
separator and the text qualifier can be omitted. In this case, default values are used.
The arguments are the same as the options in ‘File >> import >> Text File…’ of NetMiner.
48
Using NetMiner Script
To create by scripting an object for importing Excel data (*.xls), write the following script.
Because *.xlsx is not supported, it must be converted to *.xls beforehand.
(1): Key in string variables for the file path. Notice that ‘r’ is used to treat special characters
such as ‘\’ literally.
(2): Create an object for importing text data and assign it to ‘importer’. The arguments to the
function ‘createExcelImporter’ are file path and sheet name. The file path is the path where
the data to import is located, and is a mandatory argument. The sheet name is used to specify
the sheet to be imported from the Excel file. If the sheet name is omitted, the first sheet of
the Excel file is imported. The arguments are the same as the options in ‘File >> import >>
Excel File…’ of NetMiner.
49
4. Using NetMiner Script
After creating an import object, you can call its functions for setting the options.
As seen in 1), after creating an import object named ‘importer’, you can see a list of its
functions. The functions for setting the options are the same at the one in text file import
objects and Excel file import objects.
① Header
Before importing data, header options must be set. If they are not set, an error will occur
at data import. To set the header options, use the importer used in 1) and write the following
script.
(1): The function ‘setHeaderOption’ sets the header options for the importer. The arguments
indicate whether there is a row header and/or there is a column header, respectively. If the
data has a header, enter ‘True’; if it doesn’t, enter ‘False’. The ‘setHeaderOption’ function is
equivalent to the header options of ‘File >> import >> Text File…(or Excel File)’.
50
Using NetMiner Script
Notice that there are several cases that cause a row header or a column header to be
included or not. In case simple index not used as data are used in node set, edge list, or
linked list, and in case of matrix data that include labels, a row header is included. There is a
column header to be included in case there are node set attributes included or in case of
matrix data that include labels. The example below illustrates how header options set by
datatype.
[Example of header]
Data is in the form of node sets and contain attributes for each node; therefore the options
entered should be (False, True).
Data is in the form of edge lists and there are no index for both rows and columns, only
data; therefore the options entered should be (False, False).
John David 4
John Anthony 2
51
4. Using NetMiner Script
John Steven 1
John Jessica 3
John Elizabeth 5
Thomas David 5
Thomas Anthony 1
Thomas Susan 3
Thomas Elizabeth 4
Anna John 3
Anna Anthony 4
Anna Ashley 1
Anna Jackson 2
James John 2
Data is in the form of matrix and include index; therefore the options entered should be
(True, True).
Data is in the form of linked lists and there are no index for both rows and columns, only
data; therefore the options entered should be (False, False).
Data is in the form of linked lists and there are no index for both rows and columns, only
data; therefore the options entered should be (False, False).
52
Using NetMiner Script
Set the type of import data (main nodeset, sub nodeset, 1-mode network, 2-mode
network). If network data are imported, set the form of data (edge list, matrix, linked list).
This should be done mandatorily because it determines the type of data to be imported.
The script for setting the import datatype uses the importer that is used in 1). See as
follows.
(1): This function is in the importer object. You can set the type and form of the data to be
imported.
The functions correspond to the data types of ‘File >> import >> Text File…(or Excel
File)’ in NetMiner. Refer to the function reference in Chapter 3 for arguments to each
function.
53
4. Using NetMiner Script
When importing data in the form of node sets or edge lists, names and types to import are
set as attributes. The importer object used in 1) is used to write the following script.
(1): This function sets data attributes, and takes row index, attribute type, and attribute name
as its arguments. The row index refers to the index of the row that specifies the attribute
name and the attribute type. The attribute type must be given in string. One out of Number,
Text, DataTime, and Time can be selected. The attribute name can be omitted, in which case
the default name is used.
With the ‘setAttributeInfo’ function you set the same things as you can in ‘More’ in the Data
Type window in File >> import >> Text File…(or Excel File) of NetMiner, as shown below.
54
Using NetMiner Script
④ Misc. options
importer.setDiscardNewlyDiscoveredNodes(); #(1)
importer.setTargetWorkfile('workFile Name', True); #(2)
(1): This function sets the workfile that is to be created as the result of importing data. The
imported data are added to the current workfile. You can set if a sub workfile is created and
you can define the name of the workfile to be created.
(2): This function excludes newly found nodes while importing data. If this function is not
called, newly found nodes are added by default.
The functions correspond to Target Workfile and Data Integrity Option in File >> import
>> Text File…(or Excel File) of NetMiner, as shown below.
- Importing data -
UNMS_3.py
(4): Function for importing data. Data are imported based on the settings in (1)~(3).
NetMiner data can be exported as a file by using a NetMiner script. It performs the same
function as the one at ‘File >> Export’ of NetMiner. It supports two file types: text files
(*.txt, *csv); and Excel files (*.xls).
If you installed NetMiner 4 with the default settings, the file ‘01.Org_Net_Tiny1.nmf’ can
be found in the folder ‘C:\Users\(Username)\NetMiner\SampleData\Cyram
Dataset\01.Org_Net_Tiny1’.
Writing a script for exporting data is segmented roughly into three parts.
56
Using NetMiner Script
Create an exporter object to export data by using a script. The exporter object contains
functions for setting export options.
To create an exporter object for exporting NetMiner data sets to the form of text data (*.txt,
*.csv), write the following script.
(1): Set the path in a sting to which the file is exported. Notice that ‘r’ is used to treat special
characters such as ‘\’ literally.
(2): Create an exporter object for exporting text data and assign it to ‘exporter’. The
arguments to the function ‘createTextExporter’ are file path, separator, text indicator and
date/time indicator. The file path is the one to which file is imported and is a mandatory
argument. The separator specifies a character by which rows of text data is identified. By
default comma (,) is used for *.csv files while tab (‘\t’) is used for *.txt files. The text
indicator is a character by which text data are shown enclosed. By default double quotations
(“ ”) are used. The date/time indicator is a logic value, and by default it is ‘False’. If ‘True’
is used, data in the form of date and time are also shown to be enclosed by the character. The
separator, text indicator, and date/time indicator can be omitted. If omitted, defaults are used.
The arguments are the same as the options in File >> Export >> Text File… of NetMiner.
57
4. Using NetMiner Script
To create an exporter object for exporting NetMiner data sets to the form of Excel file
(*.xls), write the following script.
(1): Set the file path as a string. Notice that ‘r’ is used to treat special characters such as ‘\’
literally.
(2): Create an exporter object for exporting to a Excel file and assign it to ‘exporter’. The
arguments to the function ‘createExcelExporter’ are file path and sheet name. The file path
specifies the path to which data are exported, and is a mandatory argument. The sheet name
specifies the sheet of exported Excel data, and can be omitted. If omitted, it is automatically
set according to the export data name.
When an exporter object is created, you can use its functions for setting export options,
which are the same regardless of the type of data to be exported.
You choose and call functions based on the type of data exported (node set, matrix) and
the format of network data (edge list, matrix). You can make more specific settings using
arguments to the functions.
58
Using NetMiner Script
The script for exporting NetMiner data sets uses the exporter object used in 1), as is
shown below.
exporter.exportMainNodeSet(); #(1)
(1): This function is included in the exporter object. According to a specific function to be
used, the data to be exported and its format are set.
- exportNetworkToMatrix: Export the network data in the current workfile in the form of
matrix.
- exportNetworkToEdgeList: Export the network data in the current workfile in the form
of edge list.
The functions correspond to node set options and network options in File >> Export >>
Text File…(or Excel File) of NetMiner, as shown below. The arguments to the functions
correspond to the specific settings. Refer to the function reference in Chapter 3 for details.
- Exporting -
59
4. Using NetMiner Script
UNMS_4.py
(2): Function for setting export data type, format, and options.
You can control the data set items of the currently open
workfile. The data sets include node sets, networks, and
workfiles. Generally when a data set is deleted the
associated process log gets deleted accordingly without
confirmation.
Before running the following script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
60
Using NetMiner Script
(1): Return to “attiribute” the list of main node set attribute names of the currently open
project.
(3): Specific attribute names can be returned with its column index of array form like
‘attribute [2]’. In this case, index begins with 0.
61
4. Using NetMiner Script
Before running the following script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_6.py
(1): Return to ‘networkNames’ the list of 1-mode network names of the currently open
project.
(3): Specific network names can be returned with its index at array form like
‘networkNames [2]’.
62
Using NetMiner Script
◆ List of networks in the current ◆ Results in the log window after running the
workfile script
Before running the following script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_7.py
(1): Function for returning at a form of list the workfile names list of the currently open
project.
(2): Function for returning at a form of string the active workfile name of the currently open
project.
63
4. Using NetMiner Script
◆ Current workfile ◆ Results in the log window after running the script
2) Deleting data
Before running the following script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_8.py
(1): Function for deleting the row ‘Manager’ in the sub node set ‘Clubs’ in the currently
open project.
64
Using NetMiner Script
Before running the following script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_9.py
(1): Function for deleting network data ‘work interact’ in the currently open project.
3) Activating a workfile
Before running the following script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
65
4. Using NetMiner Script
_NM.Workfile.selectWorkfile('01.Org_Net_Tiny1'); #(1)
UNMS_10.py
(1): Provide a string of the workfile name as an argument. Runinng the script will activate
the workfile specified.
Scripts for using NetMiner analysis module sessions under Script Workbench are
automatically generated through the process tab of the script generator. As the control panel
of the script generator is organized at the same way as the per-session process tab of
NetMiner, it provides a very convenient environment in writing scripts for processing
sessions.
To process sessions the environment has to set up and executed. Output session reports
can be added and saved. In addition, process logs can be managed.
1) General session
66
Using NetMiner Script
The control panel for controlling processing sessions (General) of NetMiner obeys the
following rules in general.
2. A process tab has five items: Filter, Input, Pre-process, Main process, Post-process,
and Output.
3. Filter, Input, and Output are present for all sessions, while other items are dependent
on a particular session.
4. According to the output report of a session, the items of Display, Select, Inspect are
activated except the process tab.
The script generator of a general session is segmented into two parts, an area for selecting
a session and a process tab specialized for the particular session. The process tab has the
following items: Filter, Input, Pre-process, Main process, Post-process, and Output.
67
4. Using NetMiner Script
※ The process tab of the script generator is set up differently at session by session;
unnecessary items are omitted.
2) Visualization session
A visualization session is made up of a session that excludes Layout and Drawing items of
Visualize.
68
Using NetMiner Script
The control panel for controlling processing sessions (Visualization) of NetMiner obeys
the following rules in general.
2. Every module session has the following components: Process, Display, and Select
Tab.
3. Filter of the process tab exists in every session (Visualization); the input item exists
only in 2-mode.
The script generator of a visualization session is made up of a part for selecting the
desired session and a display tab specialized for the particular session under the layout
environment settings. The display tab is made up of Filter, Network, and Layout. Comparing
to the visualization session control panel in NetMiner, style definition is to miss in the Script
Generator.
- Automatically-entered scripts are different according to how the options are set up
in the display tab.
69
4. Using NetMiner Script
- If default settings are used without changes, the relevant option may not be written
as a script.
- Function arguments that are automatically entered can be changed according to the
user’s needs.
This is a tool that aids in writing scripts for each analysis module. It supports all modules
in Transform, Analyze, Statistics, Visualize and Chart of the main menu of NetMiner.
After selecting an analysis module and setting options through a user interface that is the
same as the control panel, the corresponding script is automatically generated. It can be
executed in the 'Script >> Script Generator' menu. The script generator can be run only
while a project is open.
If the name of analysis is entered in ‘Module Name’, the corresponding list of analysis
modules is displayed right below it. On the bottom of a module list, there are buttons called
‘Expand All’ and ‘Collapse All’. If ‘Expand All’ is clicked, all category structures of each
module are displayed; if ‘Collapse All’ is clicked, only the root category of each module is
70
Using NetMiner Script
displayed. ‘Session Variable Name’ sets variable name which is used at the session
generated by the script generator (variable whose datatype is session).
The control panel of a selected module is displayed. Set the options via the control panel
UI and press OK. A corresponding script will be generated
71
4. Using NetMiner Script
Shown below is a session script that performs degree analysis which was generated by the
script generator.
Contents of scripts generated via the script generator are classified into four parts: session
creation, session options configuration, session execution, and session report calling.
session1.setInputAdjacencyName("work interact");
session1.setAlgorithmRadioList(1); --- session options
configuration
72
Using NetMiner Script
The first line of an analysis script generated via the script generator in Script Workbench is
the function to create the session. The path to the analysis module used in NetMiner is the
argument to the function, as seen in the degree analysis session script.
The variable ‘session1’ which was assigned as a return value is in session datatype that
performs degree analysis. Accordingly ‘session1’ data can not call a function like ‘report’ at
succeeding statement, if it is not set up or performed with equicalent options.
Notice that the option setAlgorithmRadioList can be omitted if the default value (measure: #
of links) is used. However, if a setup value (measure: sum of weight) is used, the script will
be generated as above.
In addition, after the user sets up the desired option and completes the script via the session
73
4. Using NetMiner Script
74
Using NetMiner Script
If the script is run after completing an analysis session script via the script generator, the
corresponding session will be saved in NetMiner. You can call the session, get the name or
change the name after running the session.
75
4. Using NetMiner Script
Whenever you do analysis via the program, various kinds of analysis are done. In case you
what to call previously analyzed session data again, the function to use for this is
_NM.Session.openSession.
The session name can be retrieved using the name getter function for an analysis session, as
is discussed above. You can also call the desired session by looking at session tabs in
NetMiner.
We’ll work with the process log (sessions) of the current workfile. The process log items
include Session, Query Set, and Selections. Only the Session item will be examined here.
Query Set will be examined in detail in Section 4.3.
To manage the process log, the file ‘01.Org_Net_Tiny1.nmf’ is open and several sessions
are on.
76
Using NetMiner Script
Session management functions include functions that open, close, and delete sessions and
a function that returns the name of the session shown in NetMiner.
Assuming that two sessions ‘Degree’ and ‘Spring 2D’ are running in the file
‘01.Org_Net_Tiny1.nmf’, run the following script under the condition that the session
window is not open.
_NM.Session.openSession('Degree');
UNMS_11.py
77
4. Using NetMiner Script
◆ Result
-Before
- After
78
Using NetMiner Script
On the other hand, running the ‘closeSession’ function will close the session. You can
also get the session currently shown on the screen, open the session, and use the ‘close’
function of the session object.
_NM.Session.closeSession('Degree');
# or
session1 = _NM.Session.getFocusedSession();
session1.close();
UNMS_12.py
The_NM basic data class provides the function NM.deleteSession(‘session name’) for
deleting a session. With the ‘close’ function, you can also delete a session by using the
‘delete’ function of a session object.
_NM.Session.deleteSession('Degree');
# or
session1 = _NM.Session.openSession('Degree');
session1.delete();
UNMS_13.py
◆ Result
The following script is an example of using functions on a session for which Save Output
Tables can be performed. It is run while the file ‘01.Org_Net_Tiny1.nmf’ is open.
(14): This function saves the table report of the current session as a data set of a new
workfile.
80
Using NetMiner Script
After running a session, a report object can be created by using a ‘getReport’ type of
function in the session object. There are four types of reports: Main Report, Table Report,
Map Report, and Chart Report. Report objects can be created for reports that can be obtained
according to the object creation and output option settings.
The script below shows functions that create report objects, assuming ‘session1’ print out
every type of reports.
The argument to ‘getReport’ functions is the output report name of the session. If omitted,
the first report of the corresponding type is returned.
(1): Function that gets a main report item of the specified name.
(2): Function that gets a table report item of the specified name.
81
4. Using NetMiner Script
(3): Function that gets a map report item of the specified name.
(4): Function that gets a chart report item of the specified name.
1) Main report
Using the function getMainReport, a report item is returned to a report object. Two report
formats are available, which can be set by using a function of a main report object.
① getStringTable(dataName)
This function returns a specific table in the main report in the form of a string table. The
arguments include ‘Information’ or a string that describes the table or a value, which is
shown on top of the table or value. This string is in gray, and is underlined.
82
Using NetMiner Script
The script below returns the contents of the information table as a string table by
providing ‘Information’ as the argument.
The following functions can be called by the object returned in string table. String table is
classified into two, which are ‘save to’ function that exports to an Excel or a text (csv) file
and ‘set’ function that manages the exported contents.
The following script exports the main report of ‘Node Betweenness Centrality’ as an
Excel file. Before running the script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
83
4. Using NetMiner Script
UNMS_15.py
(1)~(7): This automatically-generated script by the script generator runs ‘Node Betweenness
Centrality’.
(8)~(9): This creates an object that has a report in the form of string.
(10)~(11): This specifies the path at which the Excel file is saved, as well as the sheet name.
(13): Add the report of (9) to the Excel file saved in (12). When you open the file, you can
see that it is appended to the results of (8).
84
Using NetMiner Script
② getHtmlData( )
This function converts the contents of the main report to HTML data. The argument is the
same as the stringTable function and if omitted, the entire report is converted to HTML. The
object converted to HTML mode can be viewed through ‘openHtmldialog’. The script below
is provided as an example. The file ‘01.Org_Net_Tiny1.nmf’ must be open before running.
UNMS_16.py
85
4. Using NetMiner Script
(1)~(7): This automatically-generated script by the script generator runs ‘Node Betweenness
Centrality”.
Running the script will open the HTML dialogu box as shown below.
2) Table report
Using the function ‘getTableReport’ you can return a report item to ‘tableReport’ object,
and using the object’s functions you can add to a data set or export to an Excel or text file
(csv).
86
Using NetMiner Script
① ‘addTo’ function
The ‘addTo’ function performs the same thing as is done through Data>>Add Output
Tables. Each ‘addTo’ function is determined by data set the current report is added to, and
the function can be used for sessions with a table report.
The script below shows how to add a table report as an attribute of a node set. Before
running the script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
87
4. Using NetMiner Script
(1)~(7): This automatically-generated script by the script generator runs ‘Node Betweenness
Centrality’.
(8): As the argument to (9), the attribute name of the output table must be entered, but as it is
not known the names of users and how many they are, only data are received.
(9): The table report of ‘Betweenness Centrality’ is calculated based on each node, so add as
an attribute of the main node set. The argument is the attribute name of the table report. Here,
as there is only one attribute, it is given as ‘nameList[0]’, the first item of the list retrieved in
(8).
The script below shows how to add a report to a 1-mode network. Before running the
script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
(1)~(6): This automatically-generated script by the script generator runs ‘PFNet’. The table
report from (5) is used.
(7): Add the table report of (5) to a 1-mode network. The table report of PFNet is matrix
data. As the argument to ‘addTo1modeNetwork’, the name of the network added is given.
After running the script, you can see that the network ‘PFNet’ has been added.
88
Using NetMiner Script
※ Other ‘addTo’ functions can be used in the same way as the above.
② getStringTable( )
This function is the same as the one of the main report. It is used when a table report is
exported to an Excel or a text (csv) file. The script below shows how to export the table
report of ‘Node Betweenness Centrality’ to an Excel file. Before running the script, the file
‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_19.py
89
4. Using NetMiner Script
(1)~(7): This automatically-generated script by the script generator runs ‘Node Betweenness
Centrality’. The table report from (5) is used.
(9): Function that sets the title of the table report string table.
3) Map report
The report item is returned to the report object using the getMapReport function. The
functions of the mapReport object are as follows.
90
Using NetMiner Script
The map report provides functions for saving a map to an image, for setting the type of
layout and drawing of a map, for setting the styling according to node attributes, and for
creating a node list which allows access to the nodes in order.
① saveToImage
This function performs the same thing as is done through Map >> Capture Map >> To
File of NetMiner. The results are saved as an image file at the specified path. The script
below is provided as an example. Before running the script, the file ‘01.Org_Net_Tiny1.nmf’
must be open.
91
4. Using NetMiner Script
(1)~(7): This automatically-generated script by the script generator runs ‘Node Betweenness
Centrality’.
(8): This saves the map report of (6) as an image. The arguments are the path at which the
file is saved and the resolution. The resolution can be omitted, in which case the default
resolution of 1024x768 is used.
② nodeAttributeStyling
Using this function you can set the shape, color, and size according to the partition by
referring to the main node set attributes. The script below is provided as an example and to
be run under the condition that the file ‘01.Org_Net_Tiny1.nmf’ is open.
UNMS_21.py
92
Using NetMiner Script
(1)~(13): This automatically-generated script by the script generator runs ‘Spring 2D’.
14): This function changes the shape of node by referring to the node partition of ‘Gender’
attribute. ‘Male’ is shown as a triangle while ‘Female’ is shown as a circle.
(15): The map is redrawn to reflect the new node styling. The function ‘Redraw’ must be
executed for node shapes to change on the map.
③ getNodeList
This function returns a list of nodes so that they can be accessed in order. The return value
can be used in array form or through applying iteration onto components of a list, that is
each node.
93
4. Using NetMiner Script
The ‘Node’ object functions are segmented into two groups: ‘get’ functions and ‘set’
functions. The ‘get’ functions return such things as the index of a node, label, label display
position, and whether a node is selected. The ‘set’ functions set such things as the label of a
node, label display position, node size, color and shape. In addition, a specific node on the
map can be selected.
The example script below shows how to use the above functions in a comprehensive way.
According to the gender, which is a node attribute, the label of nodes is changed to either
‘Male’ or ‘Female’, the shape of nodes either a square or circle, and the color of nodes either
black or white. Furthermore, nodes with the name ‘Anna’ are selected on the map. Before
running the script, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
94
Using NetMiner Script
UNMS_22.py
(1)~(13): This automatically-generated script by the script generator runs ‘Spring 2D’.
(15): This function returns a list of node genders for the main node set. The indexes are
consistent with those of the node list in (14). Using this list each node can be set according
to its gender.
(16)~(18): This returns the index of a node with the label ‘Anna’.
(19)~(28): This sets the node styles according to the gender by using the functions of the
‘node’ object.
(29): The contents are redrawn to reflect the node styles set. Only after calling the ‘Redraw’
function the node shapes will be changed on the screen.
4) Chart report
The chart report returns the report item to the ‘report’ object using the getChartReport
function. The function of the chartReport object saves the chart as an image, as shown below.
① saveToImage
This function perfoms the same thing as is done through Map >> Capture Map >> To File
of NetMiner. The script below is provided as an example. Before running the script, the file
‘01.Org_Net_Tiny1.nmf’ must be open.
96
Using NetMiner Script
UNMS_23.py
(1)~(4): This automatically-generated script by the script generator runs ‘Pie-Chart’. The
report from (4) is used.
(5): This function saves the chart report as an image file. The arguments are the path at
which the image file is saved and the resolution. The resolution can be omitted, in which
case the default resolution of 1024x768 is used.
97
4. Using NetMiner Script
The query functions used in the script performs the same thing as is done through
‘Tools>>Query Composer’ from the NetMiner menu. The query functions corresponding to
‘Query Composer’ are shown below.
Write a script for writing and creating a query that the user wants. The procedures for
creating a query set are as follows.
(1): Create a query object with the name ‘Age >= 30’.
98
Using NetMiner Script
Step 2: Set the query of the data set using the ‘set’ functions of the query object.
(2): The argument to the function ‘setQuery’ is the query statement used when writing a
query in ‘Query Composer’. The query is enclosed by single quotations (‘ ’ ).
After running the above script, you can see that a query with the name ‘Age>=30’ has
been added to the query set.
There are two ways of using a created query set: using the filter of the session module;
and creating a new node set workfile corresponding to the query conditions using the
‘runQuerySet’ function.
Create a query set and use it in the filter. Before running the script, the file
‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_24.py
Results of running the script: Only the nodes that meet the query criteria are processed.
100
Using NetMiner Script
UNMS_25.py
(3): A new workfile with the name ‘newWorkFile’ is created, which is only composed of
data sets of the nodes that meet the criteria.
101
5. Scripting with matrix data
The (3,3) in Table(3,3) indicates that the currently declared matrix is a 3x3 matrix.
If you click Table(3,3) above, you can view the contents of the matrix.
102
Using NetMiner Script
When the matrix data above have been created as ‘mat’, the indexes and element values of
the rows and columns can be represented as follows.
That is, if ‘print mat[0,0]’ is executed, the value at the first row and the first column, is
printed to ‘0.7’. As the index begins at 0, the first row and the first column have index of 0.
_MAT provides functions that deal with matrix data, including those that create basic
matrixes like unit matrixes and zero matrixes (_MAT.Create), and those that create matrixes
from the network of the current data set or from node attributes (_MAT.FromNM).
103
5. Scripting with matrix data
1) Unit matrix
Create a unit matrix of size NxN in which diagnoals have the value of 1 and the others
have thevalue of 0.
UNMS_26.py
2) Zero matrix
Create a zero matrix of the specified row and column size, whose elements have the
value of 0.
UNMS_27.py
(1) : Create a zero matrix whose row size is 3 and column size is 5.
104
Using NetMiner Script
3) Random matrix
A matrix is created with the specified row and column sizes, and its elements are filled
with random numbers in the range between 0 and 1.
UNMS_28.py
(1) : Create a random matrix whose row size is 4 and column size is 3.
4) Diagonal matrix
Given that the size of the specified vector is N, a NxN matrix is created and the specified
vector is used for diagonal elements while the other elements are filled with 0.
UNMS_29.py
A main node set or sub node set is returned so that it can be used in the form of a
matrix in scripting.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
106
Using NetMiner Script
UNMS_30.py
(2): Return the ‘job-ranking’ attribute of the main node set as to a Nx1 matrix.
(3): Return the ‘Gender’ attribute of the main node set as to a Nx1 matrix. If the value is
data, not number, same number is assigned to the same data.
(4): Return the ‘Members’ attribute of a sub node set ‘Clubs’ as a matrix.
2) Getting networks
A 1-mode network or a 2-mode network is returned in matrix form so that it may be used
in a script.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_31.py
(1): Return the ‘Interested In’ network of the ‘Interest Items’ sub node set as a matrix.
※ When get1modeNetwork( ) is used, the name of the 1-mode network is the only
argument.
108
Using NetMiner Script
◆ Network data (Interest Items > Interested In) ◆ Log window results
When adding a Nx1 matrix as a node set of the current workfile, N and the main node set
size must be equal in the case of adding to the main node set. On the other hand, N and the
sub node set size must be equal in the case of adding to the sub node set.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_32.py
(1) : Create a random matrix of Nx1 where N is 22. N must equal to the size of the main
node set.
109
5. Scripting with matrix data
(2) : Add the random matrix created in (1) as a main node set attribute.
Results)
2) Adding to networks
When adding a matrix as a network data set of the current workfile, there are some
limitations. In the case of 1-mode networks, only NxN matrixes where the size of the main
node set is N can be added. In the case of 2-mode networks, only matrixes that have the
same row and column sizes as the 2-mode network can be added.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
UNMS_33.py
110
Using NetMiner Script
(1): Create a random NxN matrix where N is 22. N must equal to the size of the main node
set.
Results)
1) ‘get’ functions
The size of matrixes is returned or the sum of the elements of a matrix is calculated
and returned. Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must
be open.
111
5. Scripting with matrix data
UNMS_34.py
(1): Get ‘work interact’, a 1-mode network data set, as matrix data.
(6): Return the value at row 0 and column 2 of the matrix. Zero-based index is used.
(7): (6) has been expressed in a different form. Return the value of the element at row 0 and
column 2.
112
Using NetMiner Script
(9): Return the sum of all elements below the diagonal of the matrix.
(10): Return the sum of all elements above the diagonal of the matrix.
(12): Return a vector containing the sum of each row of the matrix.
(13): Return a vector containing the sum of each column of the matrix.
2) Matrix iterator
Using a function that creates an iterator that iterates through elements whose value is not
0, matrix elements can be returned or set.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
113
5. Scripting with matrix data
UNMS_35.py
(1): Get ‘Interested In’, a 2-mode network data set, as matrix data.
(2): Create an iterator that will iterate through non-zero elements of the matrix.
(3): Items provided by the iterator are row index, column index, and value.
(4)~(6): Return the row index, column index, and value to each respective variable.
(7): Iterate through elements whose value is not 0 and print the row index, column index,
and value.
Log window results: Row index, column index, and value of elements whose value is not 0
are printed.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
114
Using NetMiner Script
UNMS_36.py
(1): Get ‘Interested In’, a 2-mode network data set, as matrix data.
(2): Change the value of the element at row 0 and column 0 to 2.0.
(3): This is another way of changing the value of an element. The value of the element at
row 0 and column 1 is changed to 2.0.
1) Matrix transformations
115
5. Scripting with matrix data
2) Matrix operators
Basic matrix operations is performed on a matrix using operators such as +, -, and *. The
‘elementWise’ function perform operations between pair of elements.
UNMS_37.py
116
Using NetMiner Script
Mat2
Let’s put network data into a matrix, perform operations, and change the resultant matrix
to network data again. The analysis process is as follows.
2) Estimate Average: Add all elements of the matrix obtained in 1) and divide by the
number of links (not 0) to get the average.
3) Use average value as a threshold: Among the elements of the matrix obtained in 1),
those with values less than the average found in 2) are processed as 0 and added to the
1-mode network.
4) Community analysis: Perform community analysis on the 1-mode network created in 3).
118
Using NetMiner Script
matrixSum.addTo1modeNetwork('average_weight2'); #(9)
UNMS_38.py
119
5. Scripting with matrix data
(1): Save the file names of imported 1-mode networks to the variable ‘networkList’ as a list
type.
(3): Add the elements of the remaining four 1-mode networks to matrixSum.
(4): Find the total sum of the elments of matrixSum from (3).
(5): Return the number of elements from matrixSum whose value is not 0.
(6): Divide the results of (4) by the results of (5) to find the average.
(8): If any of the elements of matrixSum has a value that is less than the average of (6), it is
treated as 0.
(9): Add the matrixSum of (8) to the 1-mode network as the main.
(10): Using the session helper, do community analysis of the 1-mode network of (9).
You can obtain the following analysis results after running the script.
120
Using NetMiner Script
121
6. Using Python
6. Using Python
NetMiner Script is designed based on Python for programming. It allows you to write
algorithms using Python, as well as programs that make use of Python features. In addition
to that, NetMiner’s analysis features can be used for a complementary purpose. For details
on programming with Python, refer to Appendix C.
6.1.1 Conditionals
1) if statement
The if statement includes logic expressions that compare data. A decision is made based
on the results of the comparison.
if condition:
statement
2) if - else statement
The else statement is used in combination with the if statement. The else statement
includes a block of code that gets executed if the condition is 0 or False.
If the condition is true, ‘statement1’ gets executed; if false, ‘statement2’ gets executed.
if condition:
statement1
else condition:
statement2
122
Using NetMiner Script
a=4
b = a,' is not 3'
if ( a == 3 ):
print a
else:
print b
UNMS_39.py
3) elif statement
The elif statement checks against multiple conditions for True. As soon as a condition is
True, the corresponding statement gets executed.
if condition:
statement1
elif condition:
statement2
else condition:
statement3
123
6. Using Python
score = 100
if score == 200:
print "1 st grade! "
print score
elif score == 150:
print "2 nd grade!"
print score
elif score == 100:
print "3 rd grade!"
print score
else:
print "etc...."
print score
UNMS_40.py
6.1.2 Loops
1) while statement
The while statement repeatedly executes a statement until the condition is evaluated as
false. The condition is a logical condition, which can be True or False.
while condition:
statement
124
Using NetMiner Script
i=1
while (i <= 7):
print i
i=i+1
UNMS_41.py
Results))
2) for statement
Using the for statement, you can process items in a sequence such as a list or a string.
statement
Initially, ‘target’ is assigned with the first item in ‘sequence’ and the statement is executed.
Then ‘target’ is assigned with the second item in ‘sequence’ and the statement is executed,
and then it is assigned with the third item, and so in.
e.g.:
for x in [0,1,2,3,4]
print x
UNMS_42.py
125
6. Using Python
Results:
If break or continue is used inside of a while or for loop, the loop is either exited or the
current iteration is skipped, respectively.
if condition:
statement
break
if condition:
statement
continue
i = 0;
while (i <= 7):
i=i+1
if (i == 4):
continue
if (i == 6):
break
print i
UNMS_43.py
126
Using NetMiner Script
When multiple network data sets are analyzed, a list of network data can be returned and can
be iterated through using a loop to perform the same analysis on different types of network
data.
The script below performs ‘Degree’ analysis on five different imported network data sets.
UNMS_44.py
(1) The variable ‘networkNameList’ is a list of all imported network names in the currently
focused workfile. It is in the form of [‘work interact’, ‘personal friend’, ‘personal help’, ‘personal
knows’, ‘work help’].
(2) A for loop is executed on a list data to perform the same analysis.
127
6. Using Python
The variable ‘networkName’ is assigned ‘work interact’, ‘personal friend’, ‘personal help’, ‘personal
knows’, and ‘work help’ each time through the loop.
(3) This sets the network input option. Using the variable ‘networkName’, the degree
analysis is done on the five sets of network data.
The results of running the above script are as follows.
Name of the network data used as input data of the main report.
128
Using NetMiner Script
To import multiple external data at once, Python syntax needs to be put to use. This section
shows how easily the user can import multiple external data of the same format by using a
script.
First, the data to import are edge list in a csv file, as shown below.
Given that there are 10 sets of network data in the form as above, in files Network1.csv,
Network2.csv, …. Network10.csv, the script below is written.
path = r'C:\sample'
fileName = 'Network'
fileType = 'csv'
(1) As the network files are named from 1 to 10, range(1,11) is used so that it retuns the
variable ‘num’ from 1 to 10 in order. Here, range(1,11) is the same as a list of
[1,2,3,4,5,6,7,8,9,10].
(2) The ‘num’ variable is used to increment number of filename in sequence like
‘Network1.csv’, ‘Network2.csv’. The total string is returned to the variable ‘filepath’.
129
6. Using Python
(3) ~(6) This is for importing data to NetMiner. Refer to (2) of Section 4.1.1 for details.
Using loops and conditionals appropriately, you can style individual nodes. The script
below examines nodes one at a time in the network and if the gender attribute is male, it is
colored blue, and if female, it is colored red.
index = 0;
for node in nodeList: #(3)
gender = genderList[index]
index = index + 1;
if gender == 'Male':
node.setColor(0, 0, 255)
else:
node.setColor(255,0,0)
UNMS_46.py
(1) With the file ‘01.Org_Net_Tiny1.nmf’ open, draw Spring Map using the script
generator.
(2) Return a list of nodes on the map to nodeList and return a list of genders of the nodes
to genderList.
(3) Use a for loop so that a conditional can be performed on each node in the list. Inside
of the for loop, if the gender of the node is male, the node’s color is made blue; if not,
it is made red.
131
6. Using Python
The ‘def’ statement is a compound construction that creates a new function, and that
defines a block of code to be executed when the function is called with its name and
arguments.
def function():
statement
According to the definition above, the desired function name is given after ‘def’ and in the
brackets there are arguments if any. In addition, in Python, the statement that goes inside the
function is on the next line to the function name declaration and is tabbed.
The script below prints Fibonacci numbers below the value of ‘n’ in order. A function
named ‘fibonacci’ is used, and ‘n’ as an argument. Here, ‘n’ is used as a variable so that
numbers less than ‘n’ are printed.
def fibonacci(n):
a, b = 0, 1
while a < n:
print a,
a, b = b, a+b
fibonacci(2000)
UNMS_47.py
132
Using NetMiner Script
To call a function, just enter the function name and the arguments inside the brackets. If
there are no arguments, just write as ‘()’; if there are arguments, provide arguments of the
appropriate datatypes.
function()
def function():
statement
return value
For example, the function ‘sum’ below can be written manually and used.
def sum(a,b):
return a + b return a + b
print x
UNMS_48.py
133
6. Using Python
When importing an external module, if the external module file exists in the same folder
as the script file currently being written, the import will take place successfully. But if the
module is at a path where Python doesn’t know about, you need to specify the path manually.
If an external module to import is at a specific arbitrary path, you can call it as shown below.
import sys
sys.path.append(r”module filepath ”)
import module
As shown in the above script, you can set the import path using the ‘sys.path.append’
function on the second line. In addition, to use ‘sys’, ‘import sys’ line must be added on top.
Once an external module has been imported as described in Section 6.3.1, you can use
classes or functions in the external module.
For example, let’s suppose the functions of the external module are as follows.
def sum(a,b):
return a+b
134
Using NetMiner Script
def multiplication(a,b)
return a*b
UNMS_49.py
An example of calling the ‘testModule.py’ module in a new script and using the ‘sum’
function of ‘testModule’ is given below.
import UNMS_49
x = UNMS_49.sum(3,4)
print x
UNMS_50.py
135
7. Advanced using NetMiner Script
It is convenient for the user to use dialog boxes via NetMiner script functions for entering
file path and variables and printing messages and html data.
A dialog box is opened, which returns the path of the selected file.
UNMS_51.py
(2): The path in ‘filePath’ is provided as an argument to open the corresponding file.
① Running the script will open a dialog box in which a file can be selected, as shown
below.
UNMS_52.py
(2): Print the returned variable using a ‘print’ statement to check the directory name.
137
7. Advanced using NetMiner Script
3) Message box
This function opens a message box with a specified message. The title and contents of the
message box can be specified. One of ‘normal’, ‘yesno’, ‘alert’, or ‘error’ must be specified
as the type. Only the ‘yesno’ type will return true for ‘yes’ and false for ‘no’ according to
the user’s choice.
① ‘normal’ type
② ‘yesno’ type
138
Using NetMiner Script
③ ‘alert’ type
UNMS_53.py
④ ‘error’ type
UNMS_53.py
The user can define a dialog box, such as the one that opens a file or that sets variables.
The basic steps of writing a script for user-defined dialog boxes are as follows.
Step 2: Use a function of the created object and add the desired variable to be returned.
Step 3: A user-defined dialog box can be opened using the ‘open’ function. The results
will be returned as a list, with each result item added in order.
Step 4: Use the results returned in the form of a list in other code.
UNMS_54.py
① Running the script will open a user-defined dialog box as shown below.
140
Using NetMiner Script
② After entering values or making a choice, look at the results in the log window.
You can see that the same values as those set by the user have been printed.
You can open a dialog box that will show the current status. It can be used when you want
to know the current progress of the script. When you run the script, the Script Workbench
window will disappear, which means you can’t check the current progress in the log window.
But by using the dialog box you can check it.
Before running the script below, the file ‘01.Org_Net_Tiny1.nmf’ must be open.
141
7. Advanced using NetMiner Script
for i in range(10000):
if i < 5000:
mat2 = mat *mat;
_SYS.Dialog.showStatusDialog('process :'+str(i)); #(1)
else:
mat2 = mat * mat;
_SYS.Dialog.closeStatusDialog(); #(2)
UNMS_55.py
(1) : The number of matrix multiplication operations is received by ‘i’ and shown in the
status dialog box.
(2) : If the number of operations is above 5000, the status dialog box is closed.
When running the script, you will see a dialog box that prints the number of operations,
which increments accordingly.
You can show stringTable converted to HTML data or HTML code in a HTML dialog
box.
UNMS_56.py
(1)~(7): This automatically-generated script by the script generator runs ‘Node Betweenness
Centrality’.
(8) : This converts ‘Information’ of the main report to HTML code and returns it.
Running the script will show converted HTML data in a HTML dialog box.
You can call NetMiner scripts using an external scripting language, according to the
following flow of execution.
Notice that the external script must have a function that can run ‘NetMiner 4.exe’.
The following sections describe how to run ‘NetMiner 4.exe’ in the DOS prompt and how to
run it using a Python script.
“NetMiner 4.exe path” “path of the nmf file to be imported” -s “path of the py
script file” -x
When the above is executed in the command prompt, if NetMiner runs with a specific nmf
file open, the selected script file (.py) will run.
-x : If this option is added last, NetMiner will terminate after running the script.
NetMiner Script can run with options in the DOS prompt, as described in Section 7.2.1.
Using these options, you can link with external programming languages (Python, Perl, batch
files) for execution in more diverse environments.
The following chapter gives a brief description on batch file programming and describes a
way of using NetMiner Script with Python and batch file programming.
145
7. Advanced using NetMiner Script
If you type ‘help command_name’ and press enter, you can see a more detailed description
of the command.
In this section we’ll take a brief look at a few of the most commonly used commands by
looking at examples.
- echo
echo on(default): Shows commands executed in the batch file.
echo off: Does not show commands executed in the batch file; only the results of the
commands are shown.
dir
After running:
C:\>echo off
dir contents
If ‘echo off’ is changed to ‘@echo off’, even the ‘echo off’ command itself is hidden.
- pause
The ‘pause’ command waits for the user to hit any key.
- argument
You can provide arguments for the batch file. For example, ‘test.bat [a] [b]’.
This is equivalent to arguments of Python functions.
@echo off
echo %1
dir %2
After executing test.bat aaaa *.exe
C:\test.bat TestMessage *.exe
TestMessage
(dir *.exe contents ...)
- if statement
This is similar to the ‘if’ statement in Python. When the condition is true, the commands
147
7. Advanced using NetMiner Script
- for statement
Example of using ‘for’ (test.bat)
@echo off
for %%i in ("*.bat") do (
echo %%i
)
After executing ‘Test.bat’
A list of ‘.bat’ files is printed.
When analyzing multiple nmf files, if multiple data sets can’t be imported at once due to
memory constraints or analysis has to be done with multiple nmf files already, using a batch
file can be useful.
To run NetMiner scripts in the command prompt, you need two files below, as with Section
7.2.1.
- The nmf file to import
- The script file to use
148
Using NetMiner Script
To repeatedly perform the above processing, write a batch file using a for loop.
multiple_nmf.bat
@echo off
Based on running NetMiner from the DOS prompt, you can make a script scheduling
program by using Python to run desired data at a desired time.
You can make a Python scheduling program that runs a batch file at a specified time.
The following is a bare-bones scheduling program written in Python.
import os
import time
class ScheduleRunClass():
def start(self):
print "==================================="+'\n'
print "Set the time to run the script."+'\n'
print "==================================="+'\n'
print "What day to run every week? " +'\n'
print "(0: Monday, 1: Tuesday, 2: Wednesday, 3: Thursday, 4: Friday, 5:
Saturday, 6: Sunday)? : "
s_wday = int(raw_input())
print "Which hour to run? : "
149
7. Advanced using NetMiner Script
s_hour = int(raw_input())
print "Which minute to run? : "
s_min = int(raw_input())
while(True):
print '===== Scheduling work began ======'
local_hour = time.localtime()[3]
local_min = time.localtime()[4]
local_wday = time.localtime()[6]
if local_wday == s_wday:
if local_hour == s_hour:
if local_min == s_min:
print '====Program is being run ====='
cmd = r'C:\sample\multiple_nmf.bat
os.system(cmd)
print '===== Program ended ======'
ScheduleRunClass().start()
UNMS_56.py
The above script measures the current time continuously (using local_hour, local_min, and
local_wday in the while statement) and if the time matches the user-specified time (s_hour,
s_min, s_wday), shaded ‘multiple_nmf.bat’ file is run.
150
Using NetMiner Script
7.3 Plug-in
Step 2: To make a plug-in, the script file has to be saved. If Plug-in >> Make is clicked in
the Script Workbench menu without first saving the script, an error will occur, as follows.
Step 3: After writing a script, select Plug-in >> Make to bring up a window in which a plug-
in can be created. ‘Source Files’ shows the name of the Python file for creating a plug-in file
and the path at which it is saved. In ‘Plugin Option’, you can enter the path at which the
151
7. Advanced using NetMiner Script
created plug-in file will be saved, plug-in file name, author, version, and description. Also,
the run mode can be selected. The created plug-in file will have the extension ‘nmx’.
152
Using NetMiner Script
Step 2: When the Plug-in Manager window comes up, press the Add Plug-in button.
A file selection window for selecting an nmx file will come up. Select the file ‘Plug1.nmx’
created in Section 7.3.2.
Step 3: After selecting a file, you can see information of the plug-in file in the Plug-in
Information section of the Plug-in Manager window. Press the Close button to finish
registering the plug-in.
153
7. Advanced using NetMiner Script
When the mouse cursor is brought to Tools >> Plug-ins of the menu, you will see that a
plug-in item called ‘Plug1’ has been created. Clicking this will run the plug-in.
154