University of Sydney School of Computer Science COMP5048 Week 4 Tutorial By: Dr. Amyra Meidiana

University of Sydney
School of Computer Science
COMP5048 Week 4 Tutorial
By: Dr. Amyra Meidiana
1. Network analysis in Gephi
Gephi is a graph visualisation & analysis software written in Java.
1.1. Installation
1. Install Java from https://java.com/en/download/ if you haven’t already.
2. Go to the Gephi website at https://gephi.org
3. Download the appropriate installer/archive for your system.
4. Run the installer or decompress the archive.
5. Go to the installation directory and open the executable from the bin folder.
IMPORTANT: Make sure you are installing Gephi version 0.10 or newer, as previous versions may
not be compatible with Java versions later than 8.
1.2. Basics
1. Open the Gephi executable.
2. Under “Samples” on the “Welcome” window, select “Les Miserables.gexf”. Select
“Undirected” under “Graph Type”, leave other options unchanged, and click “OK”.
3. You should see a graph drawn on the main window. If not, click Window → Graph.
Try to navigate around by using right-click drag or zooming with mouse wheel.
4. The appearance (e.g. shape, colour) can be modified from the visualisation pane, which
can be expanded using the arrow button on the bottom rightmost of the Graph tab.
5. To show node labels, go to the “Labels” tab and check the “Node” checkbox.
6. A number of layout options can be chosen from the “Layout” sidebar. Select “Force Atlas”
from the drop-down menu and click “Run”.
7. The layout may have too many overlapping nodes. Click “Stop”, try increasing the
repulsion strength and/or decreasing the attraction strength, and click “Run” again.
8. You can try other layout options. (Note: “Rotate”, “Contraction”, “Expansion”, and “Label
Adjust” are not layout algorithms, but tools to modify the current layout.)
9. To create a screenshot, click the camera icon on the bottom of the Graph window.
Figure 1: Gephi graph window
1.3. Multilevel algorithm
Gephi provides the multilevel algorithms Yifan Hu (sfdp) and ForceAtlas 2 (Barnes-Hut).
1. Open block_2000.gml in Gephi.
2. Under Layout, select Fruchterman Reingold. Reduce the drawing area to 100 then click Run.
You should notice that it takes a while for the layout to converge; if the layout algorithm is
still running after 2-3 minutes, you can manually stop it.
3. Under Layout, select Random Layout and click Run to re-obtain a random initialisation.
4. Under Layout, select Yifan Hu. Reduce the optimal distance to 50 and click Run. You should
notice that the layout converges much faster than with Fruchterman Reingold.
5. If the graph “disappears” in the middle of running the layout, scroll out until you can see the
rest of the graph. If you still see some nodes on the periphery being cut off, select
Contraction under Layout and click Run until you can see all nodes.
6. Select the ForceAtlas 2 layout and run on the same graph. If the nodes are too close together,
you can increase the repulsion force by increasing the “Scaling” parameter; if they are too
far apart, you can increase the attraction force by increasing the “Gravity” parameter.
Figure 2: Yifan Hu (sfdp) layout example
1.4. Centrality analysis
1. In the Statistics side pane, under Network Overview, locate Network Diameter and click
Run. Click OK in the pop-up window to bring up the report window.
2. Click the icon with circles (“size”) next to the currently selected palette icon (“color”) and
click “Ranking”
3. The drop down should have more options. Select “Betweenness Centrality” and click Apply.
4. If you cannot see the nodes, adjust the min and max sizes, e.g. to 5 and 20 respectively.
5. You can try other analyses, such as clustering coefficient and eigenvector centrality. Click
on Run beside each function and the drop down under Ranking should have more options.
Figure 3: Drawing with sizes based on betweenness centrality
1.5. Louvain Community Detection
Gephi provides an implementation of the Louvain community detection algorithm.
1. Click File → Open and select jazz.net.
2. Under the “Statistics” tab, locate “Modularity” under Network Overview and click “Run”.
3. Under the “Appearance” tab on the left, click ”Node”, select “Ranking”, then select
“Modularity Class” from the drop down box.
4. You should see four coloured squares on the left, one for each detected community. You can
change the colours with click-hold on the squares, moving through the colour picker, and lift
the click when you’ve chosen a colour you like
5. Click “Apply”. Each node will now be coloured according to the detected communities.
6. If the community structure does not seem visible enough in the visualisation, select a layout
and modify until you get a layout that displays the community structure satisfactorily. Save a
screenshot when you’re done.
Figure 4: Drawing with colours based on modularity class
2. Network analysis in NetworkX
2.1. Centrality analysis
1. In a Python console, type: import networkx as nx, matplotlib.pyplot as plt
2. Import a sample graph: g = nx.florentine_families_graph()
3. Compute the closeness centralities: cc = nx.closeness_centrality(g)
4. You can check the closeness centrality of a vertex using its ID, e.g. cc[‘Strozzi’]. Other
centrality functions include betweenness_centrality, eigenvector_centrality
5. You can check other centrality functions functions on the official NetworkX documentation:
https://networkx.org/documentation/stable/reference/algorithms/centrality.html
2.2. k-core analysis
1. To get the k-core number of each vertex, use: core = nx.core_number(g)
2. Get the core number of Strozzi: core[‘Strozzi’]
3. You can also try nx.clustering for computing the clustering coefficient.
2.3. Community Detection
NetworkX provides implementations of a number of community detection algorithms.
1. To find communities using Louvain algorithm: cm_lv = nx.louvain_communities(g)
2. The return value is a list of sets, where each set is one community.
3. To use Label Propagation: cm_lp = label_propagation_communities(g)
4. For other community detection-related algorithms, check the documentation at:
https://networkx.org/documentation/stable/reference/algorithms/community.html#modul
e-networkx.algorithms.community
2.4. Show analysis results in visualisation
1. Compute the degrees of each node: deg = nx.degree(g)
2. As the degrees of the node are quite small, we want to scale up the node sizes. For example,
to multiply the degree values by 200: sizes = [200 * deg[n] for n in g.nodes()]
3. Pass in the sizes of the nodes to the drawing function and set the option to show the labels:
nx.draw_spring(g, node_size=sizes, with_labels=True); plt.show()
2.5. Export properties to GML
1. Open the florentine_families_organic.gml file in a text editor as a reference – for each node,
you should see a “graphics” array containing the visual properties of the vertices, such as
position, size, and colour.
2. Load the GML file: g2 = nx.read_gml(‘florentine_families_organic.gml’)
3. Check the properties of the “Strozzi” node: g2.nodes[‘Strozzi’]. You should see a dictionary
of properties, with a nested dictionary under the key ‘graphics’.
4. Try changing the listed width (‘w’) & height (‘h’) of the nodes based on the degree. For
example: g2.nodes[‘Strozzi’][‘graphics’][‘w’] = 10 * deg[‘Strozzi’]. Try changing the sizes of
all nodes according to their degree.
5. Export the contents of g2 into a GML file.
3. Network Analysis in Tulip
3.1. Centrality analysis
1. Open the Tulip executable and load the block_2000.gml graph.
2. On the left sidebar, scroll down to the “Measure” section, then to the “Graph” subsection.
3. Click the gear next to “Betweenness Centrality” and ensure that “result” is set to
“viewMetric”, then click the green triangle to compute the betweenness centrality.
4. Scroll down to the “Resizing” section and click the gear next to “Size Mapping”. Ensure
“metric” is set to “viewMetric” and “result” is set to “viewSize”.
5. Click the green triangle next to “Size Mapping” and the sizes of the vertices should change
to be proportional to the betweenness centrality. You can change the “min size” and “max
size” parameters to adjust the minimum and maximum size of nodes.
6. Experiment with trying other available functions in the “Graph” subsection.
3.2. Community detection
1. On the left sidebar, go to the “Clustering” subsection of the “Measure” section.
2. Click the gear next to “Louvain”. Note that “result” is set to “viewMetric”; while this will
not overwrite the node resizing done in the previous subsection, it will overwrite the values
written into the “viewMetric” property of the nodes. Thus, ensure you are finished with
editing the sizes as otherwise you will have to recompute the centralities from scratch.
3. Click the green triangle next to “Louvain”.
4. Scroll up to “Coloring” and click the gear next to “Color Mapping”.
5. Click on the “color scale” option and select a color scale you prefer from the “Predefined
color scale” tab, or select your own colours.
6. Click the green triangle next to “Color Mapping” and the nodes will be coloured according
to the detected communities.
4. Multivariate Data Visualisation and Analysis in R
4.1. Installation Instructions
1. Go to the R then RStudio websites at https://www.r-project.org/ and
https://www.rstudio.com/ and go to the download page.
2. Select the appropriate package for your system from both and download. Then run
theinstallers or uncompress the packages.
4.2. Scatterplot
1. Open RStudio.
2. Read in the iris dataset: df <- read.csv( "https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv") 3. Select the sepal length as the X axis: x <- df$sepal_length 4. Select the sepal width as the Y axis: y <- df$sepal_width 5. Plot the scatterplot with the species id as color: plot(x, y, col=df$species_id) 6. You should see the plot on the right side of the right side. Click “Export” then “Save as Image” to export the plot to an image. 4.2. MDS (Multi-Dimensional Scaling) 1. The (classical) MDS function of R takes as input a distance matrix containing the pairwise distances between different entries of the data. Compute the pairwise distances between data points for the iris data: iris_dist <- dist(iris[,c(1,2,3,4)]) 2. To perform MDS on the distance matrix computed for the iris data: mds <- cmdscale(iris_dist) 3. If you print out the mds variable, you will see two columns. These denote the coordinates of each data points after reducing to two dimesions. 4. Plot the MDS results, with colour based on species: plot(mds, col=iris$Species) Figure 5: Example plot of MDS results 4.3 Parallel Coordinates with R 1. Install the plotly package if you haven’t: install.packages(“plotly”) 2. Load the plotly package: library(plotly) 3. Load the parcoord.R file and examine the source code. 4. In line 9, we define the column on which we want to colour the lines based of. Note that this column needs to be numeric. 5. Starting from line 13, we define the axes to be used for parallel coordinates. For each column to be used, we define the range by getting the minimum and maximum values for that column, and we give the column name as the values parameter. 6. Select the whole code and click “Execute”. Alternatively, copy paste the whole code into the RStudio console and press Enter. 7. You can drag along each axis to highlight/filter lines, and you can drag the columns left and right to change the ordering. Figure 8: Example parallel coordinates plot NOTE: For D3 (as seen in Week 2), you can also change the ordering of the columns by changing the ordering in the input CSV file (can be edited in a spreadsheet editor, e.g. Excel). Homework Task 1 (1 mark): Visualise the facebook.gml graph using a graph layout of your choice. Using a centrality measure and community detection method of your choice, use different visual variables to show the centrality and communities on the visualisation. Task 2 (1 mark): Load the mtcars dataset in R (df <- mtcars) and create visualisations of the following: 1. MDS plot with transmission mode (am) displayed as colour 2. Parallel coordinates with gear count displayed as colour Submission Instructions: Compile all screenshots into one PDF file and submit the PDF file to Canvas. Other file formats will not be accepted by the submission form.

发表回复