VIZBI Tutorial Session

Part 2: Cytoscape, IPython, Docker, and reproducible network data visualization workflows

Tuesday, 3/24/2015

Lesson 0: Introduction to IPython Notebook

by Keiichiro Ono


IPython Notebook is a simple tool to run your code in human-frienfly documents (Notebooks), and you can boost your productivity by learning some basic commands.

Keyboard Shortcuts

There are many keyboard shortcuts, but for now, you just need to learn the following:

  • Shift-Enter - Run cell
  • Ctrl-Enter - Run cell in-place
  • Alt-Enter - Run cell, insert below

  • Esc and Enter - Command mode and edit mode

Basic Commands in Command Mode

  • x - cut cell
  • v - paste cell below
  • SHIFT + v - paste cell above
  • dd - dlete cell

Complete list of shortcuts is available under Help menu:

OK, let's start!


In [3]:
print('Hello IPython World!')


Hello IPython World!

In [4]:
!pip list


bokeh (0.8.2)
brewer2mpl (1.4.1)
certifi (14.5.14)
chardet (2.0.1)
colorama (0.3.3)
Cython (0.20.1.post0)
decorator (3.4.0)
docutils (0.11)
Flask (0.10.1)
ggplot (0.6.5)
greenlet (0.4.5)
h5py (2.4.0)
html5lib (0.999)
ipython (3.1.0.dev0)
itsdangerous (0.24)
Jinja2 (2.7.2)
jsonschema (2.4.0)
Markdown (2.6.1)
MarkupSafe (0.18)
matplotlib (1.4.3)
mistune (0.5.1)
networkx (1.9.1)
nose (1.3.4)
numpy (1.9.0)
numpydoc (0.5)
pandas (0.16.0)
patsy (0.3.0)
Pillow (2.3.0)
pip (6.0.8)
ptyprocess (0.4)
py2cytoscape (0.3.0)
Pygments (1.6)
pygobject (3.12.0)
pyparsing (2.0.1)
pystache (0.5.4)
python-dateutil (2.4.2)
python-igraph (0.7.1-4)
pytz (2013b0)
PyYAML (3.11)
pyzmq (14.5.0)
requests (2.2.1)
roman (2.0.0)
scikit-learn (0.16.0)
scipy (0.14.0)
seaborn (0.5.1)
setuptools (3.3)
six (1.5.2)
Sphinx (1.2.2)
statsmodels (0.6.1)
sympy (0.7.6)
terminado (0.5)
tornado (4.1)
urllib3 (1.7.1)
Werkzeug (0.10.4)
yt (3.1)

In [5]:
result1 = 1+1
result2 = 2*3

In [6]:
result2
result1


Out[6]:
2

In [7]:
print('2nd = ' + str(result2))
print('1st = ' + str(result1))


2nd = 6
1st = 2

In [8]:
print('2nd = ' + str(result2))
print('1st = ' + str(result1))


2nd = 6
1st = 2

Run System Command

You can run system commands by adding ! at the beggining.

Remember: you are running this notebook in Linux container. You cannot use Windows/Mac commands even if you are using those machines!


In [9]:
!ls -alh


total 5.6M
drwxr-xr-x 1 1000 staff  374 Apr  2 00:57 .
drwxr-xr-x 1 1000 staff  340 Mar 30 19:04 ..
drwxr-xr-x 1 1000 staff  102 Mar 30 19:04 answers
drwxr-xr-x 1 1000 staff  170 Apr  2 00:57 data
-rw-r--r-- 1 1000 staff 8.1K Apr  1 18:22 .DS_Store
-rw-r--r-- 1 1000 staff 738K Mar 30 19:04 graph-tool-test.ipynb
drwxr-xr-x 1 1000 staff  170 Apr  1 18:22 .ipynb_checkpoints
-rw-r--r-- 1 1000 staff 9.9K Mar 30 19:04 Lesson_0_IPython_Notebook_Basics.ipynb
-rw-r--r-- 1 1000 staff 245K Apr  2 00:57 Lesson_1_Introduction_to_cyREST.ipynb
-rw-r--r-- 1 1000 staff 1.4M Apr  2 00:57 Lesson_2_Graph_Libraries.ipynb
-rw-r--r-- 1 1000 staff 3.3M Mar 30 19:04 Lesson_3_Visualization.ipynb

In [10]:
!ifconfig


eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:08  
          inet addr:172.17.0.8  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:8/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:1210 errors:0 dropped:0 overruns:0 frame:0
          TX packets:462 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:170312 (170.3 KB)  TX bytes:2111925 (2.1 MB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:203 errors:0 dropped:0 overruns:0 frame:0
          TX packets:203 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:51235 (51.2 KB)  TX bytes:51235 (51.2 KB)

Magic!

In IPython Notebook, there is a nice feature called magic. Magic is a group of commands to execute some usuful functions just like system commands.

Two types of Magic

  • Line magics: Prepended by one % character, only to the end of the current line.
  • Cell magics: Start with %% and applied to the entire cell

Here is the list of handy magics:

Simple performance test with %timeit


In [11]:
# Import NetworkX library, which is already installed in your Docker container
import networkx as nx

# Create a ranom graph with 100 nodes using Barabashi-Albert Model ()
ba=nx.barabasi_albert_graph(100,5)

# Check the performance of a NetworkX function (calculate betweenness centrality) by running 10 times
%timeit -n 10 nx.betweenness_centrality(ba)


10 loops, best of 3: 32.8 ms per loop

In [12]:
%%timeit -n 10

# Or, check performance of the entire cell
ws = nx.watts_strogatz_graph(100,3,0.1)
btw = nx.betweenness_centrality(ws)


10 loops, best of 3: 18.9 ms per loop

Create file manually with %%writefile


In [13]:
%%writefile data/small_network.sif
node1 is_a node2
node2 child_of node3
node3 child_of node1


Writing data/small_network.sif

In [14]:
!cat data/small_network.sif


node1 is_a node2
node2 child_of node3
node3 child_of node1

Execute under other interpreters


In [15]:
%%bash
export FOO='Env var 1'
echo $FOO


Env var 1

In [16]:
%%javascript
var foo = function(a) {
    return a+1;
};

console.log(foo(2));