Getting Started With NetMiner Script
Getting Started With NetMiner Script
Contents
1. Introduction 1
1 1.1 What is NetMiner Script?
1.1.1 NetMiner Script ............................................................................................................. 1 1.1.2 Benefits of NetMiner Script ....................................................................................... 1 1.1.2 What differentiates NetMiner Script? ...................................................................... 2
1.2 Installation
1.2.1 Installing the NetMiner 4 package ............................................................................ 3 1.2.2 Using scripts in NetMiner 3........................................................................................ 3 1.2.3 Entering NetMiner Script package license key ...................................................... 3
2. Getting Started
5
5
2.3.1 Python-based scripting ................................................................................................. 8 2.3.2 NetMiner script auto-completion .............................................................................. 8 2.3.3 Opening a project ........................................................................................................10 2.3.4 Importing data ..............................................................................................................11 2.3.5 Using script generator: degree centrality ...............................................................12
14
2.4.1 Script reuse ...................................................................................................................14 2.4.2 Looping and conditional branching ........................................................................16 2.4.3 Using external libraries ..............................................................................................19 2.4.4 User-defined functions ...............................................................................................22
-2-
1. Introduction
1.1 What is NetMiner Script?
1.1.1 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.
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.
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 2
control data and processes in a way unrelated to the GUI. In contrast, the way of writing NetMiner scripts, is the same. Therefore, while writing a NetMiner Script, the user can see in their minds 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
To use NetMiner Script, the following installation procedures are needed.
that allows for scripting. Once Tools >> Script Workbench is activated, you are ready to go to begin using NetMiner Script.
2. Getting Started
2.1 Starting and quitting Script Workbench
2.1.1 Starting
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.
2.1.2 Quitting
Click File >> Close in the Script Workbench window to quit Script Workbench. 5
2.2.1 Opening
Step 1: Run NetMiner 4 and run Script Workbench. Step 2: Click File >> Open in the menu or press the short-cut key (Ctrl+O) to open the file GS_1.py. The content of the script file will be shown in the Script Workbench as follows.
2.2.2 Running
This continues from Section 2.2.1.
Step 1: Press Ctrl+R or click Script >> Run to run the currently open script.
The above results are the same as the work interact 1-mode network which is drawn from the Kamada & Kawai layout algorithm by clicking File >> Open, opening the file 01.Org_Net_Tiny1.nmf, and executing Visualize >> Spring >> 2D session.
Step 1: Run Script Workbench. Step 2: Enter the script in the script editing window as below.
Variables can be created in the same way as a typical programming language. The end of a line is denoted by a semicolon (;), which can be omitted. Step 3: Click Script >> Run or press Ctrl+R to run the script. Step 4: Check the results in the log window.
Step 2: Click Script >> Content Assist or press the shortcut key Ctrl+Space to bring up the function list as shown below. _MAT, _NM and _SYS are unique function classes used in NetMiner Script.
In NetMiner Script, there are three function classes: _NM (NetMiner-related functions); _MAT (matrix data-related functions); _SYS (misc. functions). By adding . to a function class various classes and functions associated with that class can be used.
Step 1: In the editing window enter _MAT. and use auto-completion (Ctrl-Space) to see a list of sub classes that belong to the _MAT class.
Step 2: Select a desired function to enter it automatically as shown below. Input variables to the function are shown as well.
directory, they are located in the folders C:\Program Files\Cyram\NetMiner 4\SampleScript\Getting Started and C:\Program Files\Cyram\NetMiner 4\SampleData\Cyram Dataset\01.Org_Net_Tiny1 respectively.
Step 1: Run Script WorkBench. Step 2: Enter _NM. In the editing window, bring up a list of classes by pressing Ctrl+Space, select Project, and then select openProject(filepath) which is a function that belongs to it.
Step 3: Enter the file name, the argument to openProject(). Prefix the file path with r and enter the file path enclosed by small quotations ( ).
_NM.Project.openProject(r'C:\Program Files\Cyram\NetMiner 4\SampleData\Cyram Dataset\01.Org_Net_Tiny1\01.Org_Net_Tiny1.nmf') GS_2.py
10
Step 4: Click Script >> Run or press the shortcut key Ctrl+R to run the script. Confirm that the file 01.Org_Net_Tiny1.nmf has been opened.
Step 1: Run Script Workbench. Step 2: Write the script as follows in the editing window. You can either enter the class
and function names after . or select one in a list that is to be automatically displayed auto-completion by pressing Ctrl+Space.
importer = _NM.Import.createExcelImporter(r'C:\Program iles\Cyram\NetMiner 4\SampleData\Cyram Dataset\01.Org_Net_Tiny1\02.1-mode Network Data\00.Edge List\work interact_weighted.xls'); #-----(1) importer.setHeaderOption(False, False); #-----(2) importer.set1modeEdgeListMode('work_interact'); #-----(3) importer.runImport(); #-----(4) GS_3.py
(1): This creates an object for importing an Excel file. The argument is the file path. (2): This sets whether headers are included in the rows and columns of the data that are to be imported. As neither exists in this case, enter (False, False). (3): This sets the type of data to be imported. The argument is the name to be entered in the workfile. 11
(4): Execute the written import class and the data become imported.
Step 3: Click Script >> Run or press the shortcut key Ctrl+R to run the script. Check if network data is imported as shown below.
Step 1: Run Script Workbench. Step 2: The script generator can be executed only while the project is open. Therefore open the file '01.Org_Net_Tiny1.nmf' by clicking File >> Open in the NetMiner menu or use a 12
script to open it. If youve opened the project already by using a script, in order to prevent duplicate execution, its recommended to delete the written script before proceeding.
Step 3: Click Script >> Script Generator in the Script Workbench menu.
Step 4: The generator window will be shown when you click the script generator. To analyze Degree Centrality, select the [Analyze>Centrality>Degree] session to bring up an analysis option window that is the same as NetMiner.
Step 5: Select a desired option and click [OK]. A script for Degree Centrality analysis will be automatically generated.
13
GS_4.py
Step 6: Click Script >> Run or press the shortcut key Ctrl+R to run the script. The Degree Centrality item will be added to the session and you can see the results of analysis.
default settings, they can be found in the folders C:\Program Files\Cyram\NetMiner 4\SampleData\CyramDataset\01.Org_Net_Tiny1 and C:\Program Files\Cyram\NetMiner 4\SampleScript\Getting Started respectively.
Step 1: Open the file 01.Org_Net_Tiny1.nmf. Step 2: Run Script Workbench. Step 3: Open the file GS_4.py by clicking File >> Open. The content of the script will be shown as follows. It is the same script as the one that script generator automatically produces through a [Analyze>Centrality>Degree] analysis for the work interact network.
###-- Auto generated code by Script generator : Analyze>Centrality>Degree : Begin -session1 = _NM.Session.createSession("Analyze>Centrality>Degree"); session1.setInputAdjacencyName("work interact"); session1.run(); report1 = session1.getMainReport("[R] Main"); report2 = session1.getTableReport("[T] Degree Centrality Vector"); report3 = session1.getMapReport("[M] Spring"); report4 = session1.getMapReport("[M] Concentric"); ###-- Auto generated code by Script generator : Analyze>Centrality>Degree : End -GS_4.py
Step 4: Run the script by clicking Script >> Run. The following results will be shown.
15
Step 5: In the script, change work interact, the argument to session1.setInputAdjacencyName(work interact), to personal friend.
###-- Auto generated code by Script generator : Analyze>Centrality>Degree : Begin -session1 = _NM.Session.createSession("Analyze>Centrality>Degree"); session1.setInputAdjacencyName("personal friend"); session1.run(); report1 = session1.getMainReport("[R] Main"); report2 = session1.getTableReport("[T] Degree Centrality Vector"); report3 = session1.getMapReport("[M] Spring"); report4 = session1.getMapReport("[M] Concentric"); ###-- Auto generated code by Script generator : Analyze>Centrality>Degree : End --
Step 6: Run the modified script to see the results of Degree Centrality for the personal friend network.
Step 7: Change the input data and run the script again.
files
01.Org_Net_Tiny1.nmf and GS_5.py are used. If you installed NetMiner 4 with the default settings, they can be found in the folders C:\Program Files\Cyram\NetMiner 4\SampleData\Cyram Dataset\01.Org_Net_Tiny1 and C:\Program Files\Cyram\NetMiner 4\SampleScript\Getting Started respectively.
Step 1: Open the file 01.Org_Net_Tiny1.nmf. Step 2: Run Script Workbench. Step 3: Open the file GS_5.py by clicking File >> Open. The content of the script displays as follows.
networkNameList = ['work interact', 'personal friend', 'personal help', 'personal knows', 'work help']; for networkName in networkNameList: if (networkName == 'personal friend'):
###-- Auto generated code by Script generator : Analyze>Centrality>Betweenness>Node : Begin --
session1 = _NM.Session.createSession("Analyze>Centrality>Betweenness>Node"); session1.setInputAdjacencyName(networkName); session1.run(); report1 = session1.getMainReport("[R] Main"); report2 = session1.getTableReport("[T] Node Betweenness Centrality Vector"); report3 = session1.getMapReport("[M] Spring"); report4 = session1.getMapReport("[M] Concentric");
###-- Auto generated code by Script generator : Analyze>Centrality>Betweenness>Node : End --
else:
###-- Auto generated code by Script generator : Analyze>Centrality>Degree : Begin --
session1 = _NM.Session.createSession("Analyze>Centrality>Degree"); session1.setInputAdjacencyName(networkName); session1.run(); report1 = session1.getMainReport("[R] Main"); report2 = session1.getTableReport("[T] Degree Centrality Vector"); report3 = session1.getMapReport("[M] Spring"); report4 = session1.getMapReport("[M] Concentric");
###-- Auto generated code by Script generator : Analyze>Centrality>Degree : End
GS_5.py
17
Analysis is repeated using the for construct, and conditional branching is done using the if construct. In the above script, networkNameList is a list of 1-mode network data names that exist as data sets in 01.Org_Net_Tiny1. networkName, an item of the list, returns as an argument, to the analysis scripts of Degree Centrality and Node Betweenness Centrality which were automatically generated by using the script generator. When above process is proceeding, the if construct branches personal friend to Node Betweenness Centrality analysis while others to Degree Centrality analysis. Step 4: Run the script by clicking Script >> Run.
As a result of running the script, an analysis session on five networks (work interact, personal friend, personal help, personal knows, work help) is created. For the second network, namely personal friend, Node Betweenness Centrality was performed, while for the rest Degree Centrality was performed in order.
18
Step 1: Run Script Workbench. Step 2: Open the file GS_6.py by clicking File >> Open. The content of the script will be shown as follows. # -*- coding: utf-8 -*import csv import api as api_ import time import os, sys s_time = time.time() custom = _SYS.Dialog.createCustomDialog('Crawling Box','Option') custom.addStringVariable('Seed User Name :','evegood99') custom.addComboBoxVariable('Select Relation :',['Followers']) custom.addIntegerVariable('Depth :',1) custom_result = custom.open() seedName = custom_result[0] relation = custom_result[1] netDepth = custom_result[2] api = api_.Twitter() def searchFollowers(seed): followersList = [] try: result = api.statuses.followers(screen_name=seed) for follower in result: 19
def makeLinkListCSV(linkList): try: os.mkdir(r'c:\tmp_link') except: pass fpout = open(r'c:\tmp_link\tmp_tweet_linkList.csv','w') csv_writer = csv.writer(fpout, lineterminator='\n', quotechar='"', quoting=csv.QUOTE_ALL) for edge in linkList: csv_writer.writerow(edge) fpout.close()
def main(): seedQueue = [seedName] nodeList = [seedName] linkList = [] nodeDepth = {} nodeDepth[seedName] = 0 endSeed = {} while (len(seedQueue)!=0): seed = seedQueue.pop(0) _SYS.Dialog.showStatusDialog(str('['+seed+']'+' followers searching...')) followersList = searchFollowers(seed) endSeed[seed] = True for fol in followersList: linkList.append([fol,seed]) if nodeDepth.has_key(fol): pass else: nodeDepth[fol] = nodeDepth[seed] + 1 nodeList = list(set(nodeList + followersList)) for node in nodeList: if nodeDepth[node] <= netDepth and endSeed.has_key(node) == False: 20
seedQueue.append(node) seedQueue = list(set(seedQueue)) lastEdgeList = [] for edge in linkList: if nodeDepth[edge[0]] <= netDepth and nodeDepth[edge[1]] <= netDepth: lastEdgeList.append(edge) makeLinkListCSV(lastEdgeList) _SYS.Dialog.showStatusDialog(str('Importing.... please wait')) linkImport = _NM.Import.createTextImporter(r'c:\tmp_link\tmp_tweet_linkList.csv') linkImport.setHeaderOption(False, False) linkImport.set1modeEdgeListMode('follower_network') linkImport.runImport() main()
GS_6.py
The BFS algorithm is used in designing main function. Starting with a seed node BFS algorithm enables to find the followers using the function searchFollowers, and then followers of followers in sequence. Inside the searchFollowers function the Twitter API is used. Finally to import to NetMiner, a temporary csv file is created (in makeLinkListCSV function) and the file is imported to NM.
Step 3: Click on Script >> Run to bring up a dialog window where we can input the twitter screen name as the starting point of data collection and the depth level of collection can be entered.
21
Enter the screen name for Seed User Name and enter the depth of collection for Depth. Press OK to start crawling. When the crawling is completed, collected data is displayed.
Visualizing the above data, the following Twitter relationship map is drawn.
Step 1:
Step 2: Run Script Workbench. Step 3: Open the file GS_7.py by clicking File >> Open to bring up the following script.
def average(list): tot = 0.0; num = 0; for value in list: tot = tot + value; num = num + 1; avg = tot / num; return avg; def stddev(list): sum = 0.0; num = 0; avg = average(list); for value in list: sum = sum + (value - avg)**2; num = num + 1; std = (sum**0.5)/num; return std;
###-- Auto generated code by Script generator : Analyze>Centrality>Betweenness>Node : Begin -
session1 = _NM.Session.createSession("Analyze>Centrality>Betweenness>Node"); session1.setInputAdjacencyName("work interact"); session1.run(); report1 = session1.getMainReport("[R] Main"); report2 = session1.getTableReport("[T] Node Betweenness Centrality Vector"); report3 = session1.getMapReport("[M] Spring"); report4 = session1.getMapReport("[M] Concentric");
###-- Auto generated code by Script generator : Analyze>Centrality>Betweenness>Node : End --
attName = report2.getAttributeNames(); report2.addToNodeAttribute(attName[0], 'betw'); betwList = _NM.Dataset.getMainAttributeValues('betw') print 'avg : ', average(betwList); print 'std : ', stddev(betwList);
GS_7.py
23
In the above script a function for finding the average and the standard deviation is defined by the user. After measuring Node Betweenness Centrality, the average and standard deviation of the centrality is found by using the user-defined function. The functions average and stddev find the average and the standard deviation respectively. Arguments to each function are made in a form of a list after adding as a main node attribute by measuring the networks Node Betweenness Centrality. Step 4: Run the script by clicking Script >> Run. In the log window, the average and standard deviation of centrality values will be shown.
24