-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintroduction.tex
384 lines (287 loc) · 9.26 KB
/
introduction.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
\begin{frame}
\frametitle{}
\begin{center}
{\fontsize{49}{90}\selectfont
{\color{title} Introduction}}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Example: social networks and epidemics}
Understand epidemic outbreak of diseases through modeling
Build social networks from detailed census data
Run dynamic models for smallpox, SARS, flu, etc.
\begin{columns}[c]
\begin{column}{0.65\textwidth}
\centerline{\includegraphics[width=1.0\columnwidth]{socialnet}}
\centerline{\small Building a social network}
\end{column}
\begin{column}{0.35\textwidth}
\centerline{\includegraphics[width=1.0\columnwidth]{1311774-2c}}
\centerline{\small Social network of one person}
\end{column}
\end{columns}
Goal: find a good intervention strategy
\footnotesize
If Smallpox Strikes Portland...\\
by: Chris L. Barrett, Stephen G. Eubank, James P. Smith
Scientific American (March 2005)
\end{frame}
\begin{frame}
\frametitle{Goals: Why we started project}
\begin{block}
{We needed:}
\begin{itemize}
\item Tool to study the structure and
dynamics of social, biological, and infrastructure networks
\item Ease-of-use and rapid
development in a collaborative, multidisciplinary environment
\item Open-source tool base that can easily
grow in a multidisciplinary environment with non-expert users and developers
\item An easy interface to
existing code bases written in C, C++, and FORTRAN
\item To painlessly slurp in large nonstandard data sets
\end{itemize}
\end{block}
\begin{itemize}
\item No existing API or graph implementation that was suitable
\item Inspired by Guido van Rossum's 1998 Python graph representation essay
\item First public release in April 2005
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Features: NetworkX in one slide}
Python language package for exploration and analysis of
networks and network algorithms
\begin{columns}[c]
\begin{column}{0.65\textwidth}
\begin{itemize}
\item Data structures for representing many types of networks, or graphs,
(simple graphs, directed graphs, and graphs with parallel edges and
self loops)
\item Nodes can be any (hashable) Python object
\item Edges can contain arbitrary data
\item Many network science algorithms (centrality, paths, graph generators)
\item Flexibility ideal for representing networks found
in many different fields
\item Many unit and functional tests
\item Online up-to-date documentation
\item Open source software (BSD license), Github developer site
\item Works with Python 3
\end{itemize}
\end{column}
\begin{column}{0.35\textwidth}
\centerline{\includegraphics[width=1.0\columnwidth]{art_white}}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]
\frametitle{Design decisions}
\begin{block}
{NetworkX defines no custom node objects or edge objects}
\begin{itemize}
\item ``node-centric'' view of network
\item Nodes: whatever you put in (hashable) with optional node data
\item Edges: tuples with optional edge data
\item Edge, node data can be anything
\end{itemize}
\end{block}
\begin{block}
{NetworkX is all Python}
Other projects use custom compiled code (and Python): Boost Graph,
igraph, Graphviz, graph-tool
\begin{itemize}
\item Focus on computational network modeling not software tool development
\item Move fast to design new algorithms or models
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: Simple use, adding nodes}
Start Python
Import NetworkX using ``nx'' as a short name
\begin{block}{}
\begin{verbatim}
>>> import networkx as nx
\end{verbatim}
\end{block}
The basic $Graph$ class is used to hold the network information.
Nodes can be added as follows:
\begin{block}{}
\begin{verbatim}
>>> G=nx.Graph()
>>> G.add_node(1) # integer
>>> G.add_node('a') # string
>>> print G.nodes()
['a', 1]
\end{verbatim}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: nodes can be ``anything''}
Nodes can be any hashable object such as strings,
numbers, files, functions, and more
\begin{block}{}
\begin{verbatim}
>>> import math
>>> G.add_node(math.cos) # cosine function
>>> fh=open('tmp.txt','w')
>>> G.add_node(fh) # file handle
>>> print G.nodes()
[<built-in function cos>,
<open file 'tmp.txt', mode 'w' at 0x30dc38>]
\end{verbatim}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: edges are just pairs of nodes}
Edges, or links, between nodes are represented as tuples of nodes.
They can be added simply
\begin{block}{}
\begin{verbatim}
>>> G.add_edge(1,'a')
>>> G.add_edge('b',math.cos)
>>> print G.edges()
[('b', <built-in function cos>), ('a', 1)]
\end{verbatim}
\end{block}
If the nodes do not already exist they are automatically added to the graph.
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: Edge can hold arbitrary data}
\begin{columns}[T]
\begin{column}{0.7\textwidth}
Any Python object is allowed as edge data
(e.g. number, string, image, file, ip address)
Edge data assigned and stored in a Python dictionary (default empty).
\bigskip
Use Dijkstra's algorithm to find the shortest path:
\end{column}
\begin{column}{0.3\textwidth}
\centerline{\includegraphics[width=1.5\columnwidth]{dijkstra}}
\end{column}
\end{columns}
\begin{block}{}
\begin{verbatim}
>>> G=nx.Graph()
>>> G.add_edge('a','b',weight=0.3)
>>> G.add_edge('b','c',weight=0.5)
>>> G.add_edge('a','c',weight=2.0)
>>> G.add_edge('c','d',weight=1.0)
>>> print nx.shortest_path(G,'a','d')
['a', 'c', 'd']
>>> print nx.shortest_path(G,'a','d',weighted=True)
['a', 'b', 'c', 'd']
\end{verbatim}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: testing}
NetworkX has many tests that can be run by users
\footnotesize
\begin{block}{}
\begin{verbatim}
>>> import networkx
>>> networkx.test(verbosity=2)
...
Doctest: networkx.utils ... ok
Conversion from digraph to array to digraph. ... ok
Conversion from digraph to matrix to digraph. ... ok
Conversion from graph to array to graph. ... ok
Conversion from graph to matrix to graph. ... ok
Conversion from weighted digraph to array to weighted digraph. ... ok
...
Conversion from non-square array. ... ok
Conversion from digraph to sparse matrix to digraph. ... ok
Conversion from graph to sparse matrix to graph. ... ok
Conversion from weighted digraph to sparse matrix to weighted digraph. ... ok
Conversion from weighted graph to sparse matrix to weighted graph. ... ok
Conversion from graph to sparse matrix to graph with nodelist. ... ok
Conversion from non-square sparse array. ... ok
Doctest: networkx ... ok
----------------------------------------------------------------------
Ran 1798 tests in 17.202s
OK
\end{verbatim}
\end{block}
\end{frame}
\begin{frame}
\frametitle{Feature: Online, up-to-date documentation}
\begin{columns}[T]
\begin{column}{0.5\textwidth}
\centerline{\includegraphics[width=1.0\columnwidth]{networkx-home}}
\end{column}
\begin{column}{0.5\textwidth}
\centerline{\includegraphics[width=1.0\columnwidth]{networkx-doc}}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: Python expressivity - a simple algorithm}
Python is easy to write and read
\begin{block}{Breadth First Search}
\begin{verbatim}
from collections import deque
def breadth_first_search(g, source):
queue = deque([(None, source)])
enqueued = set([source])
while queue:
parent, n = queue.popleft()
yield parent, n
new = set(g[n]) - enqueued
enqueued |= new
queue.extend([(n, child) for child in new])
\end{verbatim}
\end{block}
Credit: Matteo Dell'Amico
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: Compact code - building new generators}
\centerline{\includegraphics[width=0.7\columnwidth]{model_title}}
\centerline{\includegraphics[width=0.4\columnwidth]{model}}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: Compact code - building new generators}
\begin{block}{}
\tiny
\lstinputlisting[firstline=1]{code/scale_free_graph.py}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: leveraging libraries}
Use well-tested numerical and statistical libraries
E.g. convert Graphs to and from NumPy (and SciPy sparse) matrices
Example: Find eigenvalue spectrum of the graph Laplacian
\begin{columns}[T]
\begin{column}{0.75\textwidth}
\begin{block}{}
\lstinputlisting[firstline=1]{code/laplacian.py.doctest}
\end{block}
\end{column}
\begin{column}{0.2\textwidth}
\centerline{\includegraphics[width=1.0\columnwidth]{path6}}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[fragile]
\frametitle{Feature: drawing}
Built-in interface to Matplotlib plotting package
Node positioning algorithms based on force-directed, spectral, and geometric methods
\begin{block}{}
\begin{verbatim}
>>> G = nx.circular_ladder_graph(12)
>>> nx.draw(G) # Matplotlib under the hood
\end{verbatim}
\end{block}
\centerline{\includegraphics[width=0.7\columnwidth]{ladder}}
\end{frame}
\begin{frame}[fragile]
\frametitle{Drawing with Matplotlib}
\includegraphics[width=0.25\columnwidth]{circular_tree_thumb}
\includegraphics[width=0.25\columnwidth]{degree_histogram_thumb}
\includegraphics[width=0.25\columnwidth]{edge_colormap_thumb}
\includegraphics[width=0.25\columnwidth]{four_grids_thumb}
\includegraphics[width=0.25\columnwidth]{giant_component_thumb}
\includegraphics[width=0.25\columnwidth]{labels_and_colors_thumb}
\includegraphics[width=0.25\columnwidth]{node_colormap_thumb}
\includegraphics[width=0.25\columnwidth]{random_geometric_graph_thumb}
\end{frame}