SDCSB Tutorial Session

Advanced Cytoscape: Cytoscape, IPython, Docker, and reproducible network data visualization workflows

Friday, 4/17/2015

Lesson 0: Introduction to IPython Notebook

by Keiichiro Ono


In this tutorial, IPython Notebook is the main workbench to control Cytoscape and other tools.

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

  • Enter Command Mode: ESC
  • Enter Edit Mode: ENTER
Command Mode
  • Move to cell above - k or ↑
  • Move to cell below: j or ↓

Basic Commands in Command Mode

  • x - cut cell
  • c - copy 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 [1]:
print('Hello IPython World!')


Hello IPython World!

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

In [3]:
result2
result1


Out[3]:
2

In [4]:
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 [5]:
!ls -alh


total 3.5M
drwxr-xr-x 1 1000 staff  306 Apr 15 19:42 .
drwxr-xr-x 1 1000 staff  340 Apr 15 19:41 ..
drwxr-xr-x 1 1000 staff  102 Mar 30 19:04 answers
drwxr-xr-x 1 1000 staff  306 Apr 15 00:43 data
-rw-r--r-- 1 1000 staff  11K Apr 15 18:37 .DS_Store
drwxr-xr-x 1 1000 staff  238 Apr 15 18:37 .ipynb_checkpoints
-rw-r--r-- 1 1000 staff  11K Apr 15 19:41 Lesson_0_IPython_Notebook_Basics.ipynb
-rw-r--r-- 1 1000 staff 223K Apr 15 02:11 Lesson_1_Introduction_to_cyREST.ipynb
-rw-r--r-- 1 1000 staff 3.2M Apr 15 18:24 Lesson_2_Visualization.ipynb

In [6]:
# List all installed Python packages
!pip list


You are using pip version 6.0.8, however version 6.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
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)
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.3)
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)

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 [7]:
# 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.9 ms per loop

In [8]:
%%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: 15.6 ms per loop

Create file manually with %%writefile


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


Overwriting data/small_network.sif

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


node1 is_a node2
node2 child_of node3
node3 child_of node1

Execute under other interpreters


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


Env var 1

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

console.log(foo(2));