Commit 6ec56e1c authored by Michael Lees's avatar Michael Lees
Browse files

More fixes, polishing of text, more introduction, etc.

parent d88c2049
airports.graphml
in-degree.png
out-degree.png
test.edgelist
%% Cell type:markdown id: tags:
# A Short Practical Introduction to NetworkX
### Authors: [Michael Lees](http://www.mhlees.com), Debraj Roy
---
The MIT License (MIT)
Copyright (c) 2015 Michael Lees, Debraj Roy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---
## Introduction
This small collection of Jupyter (IPython) notebooks serves as a introduction to the [NetworkX](http://) Python package. It is intended for students who are interested to run network simulation or network analysis using NetworkX and Python.
You should be able to understand and follow most of the code examples without prior programming experience. However, in order to gain most from the tutorials it is advisable to have some basic programming knowledge. If you have never programmed before I would recommend the [Python code academy course](http://www.codeacademy.com/python). There is also an assumption that you understand something about Complex Networks (this practical course has a corresponding series of lectures). If you would like to learn more about Complex Networks I would advise the free [e-book](barabasilab.neu.edu/networksciencebook/) by Laszlo Barabási.
The course consists of four Jupyter notebooks:
1. <span style="color:green">**_NetworkX Basics_**</span> - this is more or less a direct copy of the official [NetworkX tutorial](http://www.networkx.com). It will teach you about the basic forms of graphs in NetworkX, basic drawing, how to create nodes, edges, etc.
2. <span style="color:green">**_Network Data Analysis using NetworkX_**</span> - this covers an example of analysing real world data (the worldwide airline network) in NetworkX. It shows various centrality measures, how to plot the degree distribution, how to export to various file formats (that can work with Gephi), fitting plots with powerlaw, etc.
3. <span style="color:green">**_Network Robustness using NetworkX_**</span> - this repeats some classic experiments related to the robustness of scale-free networks when nodes fail or are attacked. It also includes robustness analysis of the airline network used in part two.
4. <span style="color:green">**_Epidemic Spreading using NetworkX_**</span> - this builds a simple SIR model for epidemic spreading, then investigates different vacination strategies to assess the effectiveness is prevent the disease spread.
The first step you must make is to ensure that you have all the necessary software/libraries installed on your own machine, see the below section for instructions on how to do this.
%% Cell type:markdown id: tags:
## Setup on your computer
This series of tutorials uses Python and NetworkX to analyse complex networks. Jupyter (IPython) notebooks are used through-out as they are really excellent teaching tools! In this notebook we will provide instructions for installing all the necessary software on your own machine, whether it be Windows, Mac, Linux.
### Python 2.7x
Please install Python 2.7x for your operating system. <span style="color:red">**DO NOT INSTALL Python 3.x**</span>
If you already have python 3.x installed then it's advisable to use anaconda as instructed below to install Python 2.7x.
%% Cell type:markdown id: tags:
---
## 1. **Install Python**
Perhaps the simplest way if you don't know any of the other options is to use Anaconda. It works on all OS
### Mac
a. **Mac Brew**: *brew install python*<br>
b. **Anaconda**: Read https://www.continuum.io/sites/default/files/Anaconda-Quickstart.pdf
### Windows
a. **Windows Installer**: Goto https://www.python.org/downloads/release/python-2710/ and download the correct installer (either 32-bit or 64-bit). <br>
b. **Anaconda**: Read https://www.continuum.io/sites/default/files/Anaconda-Quickstart.pdf
### Linux
a. **Anaconda**: Read https://www.continuum.io/sites/default/files/Anaconda-Quickstart.pdf
---
## 2a. Install Jupyter (skip if you used anaconda)
**If you used anaconda you can skip this step.**
From the Mac terminal, or the anaconda terminal, the linux terminal, or the windows command prompt, type the following:
*pip install jupyter*
---
## <span style="color:red">NOTE: If anything fails above see: http://jupyter.readthedocs.org/en/latest/install.html</span>
---
## 3. **Install other required libraries**
From the Mac terminal, or the anaconda terminal, the linux terminal, or the windows command prompt, type the following:
- *pip install networkx*
- *pip install matplotlib*
- *pip install community*
- *pip install powerlaw*
- *pip install scipy*
- *pip install pydot*
- *pip install python-louvain*
- *pip install tqdm*
- *pip install mpmath*
- *pip install pyparsing*
- *pip install enum*
- *pip install bokeh*
---
%% Cell type:markdown id: tags:
## Testing installation
Once everything is installed, you should be able to open this jupyter notebook and run it in the browser.
To do this open Terminal or Command prompt, navigate to the directory in which you planced this and the other python notebooks (where you saved on download). Then do:
*jupyter notebook*
A browser should open and you should see the list of notebooks. Click on **0. Setup**.
Navigate back to this point in the netbook, then select the cell below and press (shift+return) to try and execute the import commands below. If you see no errors and it prints "ALL OK!", everything should be ok. If you see some errors, please read the instructions above and try google to see if you can fix the problem yourself.
If you still don't manage you can send an email to the course organizers, including a copy-paste of your error.
%% Cell type:code id: tags:
``` python
try:
import csv #import the Python CSV library
import networkx as nx #import NetworkX
import numpy as np #import numpy for ...
import community #import community (https://pypi.python.org/pypi/python-louvain/0.3)
#force drawing of graphs inline for ipython notebook
%matplotlib inline
import matplotlib.pyplot as plt #import matplotlib for plotting/drawing grpahs
import powerlaw # for testing fits on heavy tailed distributions
import bokeh # for creating interactive plots
import copy # this is use for making deep copies of lists
from tqdm import tqdm # nice library for progress bars
import enum #for enumeration support in Python 2.7x
except ImportError:
import traceback
traceback.print_exc()
raise ImportError('Something failed, see above.')
else:
print " ........ ALL OK! ........ "
```
%%%% Output: stream
........ ALL OK! ........
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# Setup on your computer
This series of tutorials uses Python and NetworkX to analyse complex networks. Ipython notebooks are used through-out as they are really excellent teaching tools! In this notebook we will provide instructions for installing all the necessary software on your own machine, whether it be Windows, Mac, Linux.
## Python 2.7x
Please install Python 2.7x for your operating system. DO NOT INSTALL python 3.x
If you already have python 3.x installed then it's advisable to use anaconda.
You can find out how to install Python 2.7x here:
%% Cell type:markdown id: tags:
## Mac
There are two approaches, either through brew and pip, or through anaconda.
If you don't have brew intalled, or don't know what it is then use Anaconda.
### Method: Anaconda
Read https://www.continuum.io/sites/default/files/Anaconda-Quickstart.pdf
### Method: Brew and Pip
The following are all commands that should be executed in the mac terminal.
1. **Install Python**
- *brew install python*<br><br>
2. **Install iPython (include the quotes)**
- *pip install "ipython[notebook]"*<br><br>
3. **Install other required libraries**
- *pip install networkx*
- *pip install matplotlib*
- *pip install community*
- *pip install powerlaw*
- *pip install scipy*
- *pip install pydot*
- *pip install python-louvain*
- *pip install tqdm*
- *pip install mpmath*
- *pip install pyparsing*
- *pip install enum*
- *pip install bokeh*
%% Cell type:markdown id: tags:
## Windows
There are two approaches, either through manual install and pip, or through anaconda.
If you don't have brew intalled, or don't know what it is then use Anaconda.
### Method: Anaconda
Read https://www.continuum.io/sites/default/files/Anaconda-Quickstart.pdf
### Method: Install and Pip
1. **Install Python**
Goto https://www.python.org/downloads/release/python-2710/ and download the correct installer (either 32-bit or 64-bit).
The following pip commands are all issued in the command prompt. Now open the command prompt (Windows KEY + R, then type **cmd**)<br><br>
2. **Install iPython**
*pip install "ipython[notebook]"*<br><br>
3. **Install other required libraries**
*pip install networkx* <br>
*pip install matplotlib* <br>
*pip install community* <br>
*pip install powerlaw* <br>
*pip install scipy* <br>
*pip install pydot* <br>
*pip install python-louvain* <br>
%% Cell type:markdown id: tags:
## Testing installation
Once everything is installed, you should be able to open this ipython notebook within ipython on your own machine.
To do this open Terminal or Command prompt, navigate to the directory in which you planced this and the other python notebooks (where you saved on download). Then do:
*ipython notebook*
A browser should open and you should see the list of notebooks. Click on **0. Setup**.
Navigate back to this point in the netbook, then select the cell below and press (shift+return) to try and execute the import commands below. If you see no errors, everything should be ok. If you see some errors, please read the instructions above and try google to see if you can fix yourself.
If not send an email to the course organizers, including a copy-paste of your error.
%% Cell type:code id: tags:
``` python
try:
import csv #import the Python CSV library
import networkx as nx #import NetworkX
import numpy as np #import numpy for ...
import community #import community (https://pypi.python.org/pypi/python-louvain/0.3)
#force drawing of graphs inline for ipython notebook
%matplotlib inline
import matplotlib.pyplot as plt #import matplotlib for plotting/drawing grpahs
import powerlaw
except ImportError:
import traceback
traceback.print_exc()
raise ImportError('Something failed, see above.')
else:
print "ALL OK!"
```
%%%% Output: stream
ALL OK!
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment