DL Zip


In [1]:
import urllib2, zipfile

In [7]:
url = 'http://ipython.rossant.net/'
filename = 'facebook.zip'

In [8]:
downloaded = urllib2.urlopen(url + filename)

In [9]:
folder = 'data'

In [10]:
mkdir $folder

In [11]:
cd $folder


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/data

In [12]:
cd ..


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data

In [13]:
with open(filename, 'wb') as f:
    f.write(downloaded.read())

In [14]:
with zipfile.ZipFile(filename) as zip:
    zip.extractall('.')

In [15]:
cd $folder


[Errno 2] No such file or directory: 'data'
/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data

In [18]:
ls


facebook/       facebook-r.zip  facebook.zip

In [19]:
cd facebook/


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook

In [20]:
ls


0.circles     1684.circles  3437.circles  3980.circles  686.circles
0.edges       1684.edges    3437.edges    3980.edges    686.edges
107.circles   1912.circles  348.circles   414.circles   698.circles
107.edges     1912.edges    348.edges     414.edges     698.edges

In [21]:
%bookmark fbdata


In [23]:
pwd


Out[23]:
u'/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook'

In [24]:
cd ..


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data

In [25]:
cd facebook/


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook

In [26]:
with open('0.edges', 'r') as f:
    print f.readline()


236 186


System shell:


In [32]:
cd fbdata


(bookmark:fbdata) -> /Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook
/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/facebook

In [39]:
files = !ls -1 -S | grep edges

In [40]:
files


Out[40]:
['1912.edges',
 '107.edges',
 '1684.edges',
 '3437.edges',
 '348.edges',
 '0.edges',
 '414.edges',
 '686.edges',
 '698.edges',
 '3980.edges']

In [41]:
!head -n5 {files[0]}


2290 2363
2346 2025
2140 2428
2201 2506
2425 2557


In [54]:
%alias largest ls -1shS | grep %s

In [55]:
largest circles


  16 1912.circles
   8 1684.circles
   8 107.circles
   8 348.circles
   8 686.circles
   8 0.circles
   8 3437.circles
   8 414.circles
   8 698.circles
   8 3980.circles

In [56]:
%store largest


Alias stored: largest (ls -1shS | grep %s)

History:


In [57]:
2 + 3


Out[57]:
5

In [58]:
_ * 2


Out[58]:
10

In [59]:
%hist -nop 1-2


   1: >>> ls
   2: >>> pwd
u'/Users/sukruhasdemir/Dropbox/python-study/ipython-book'


In [62]:
cd ..


/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data

In [63]:
%run egos.py facebook

In [64]:
ids


Out[64]:
[0, 107, 348, 414, 686, 698, 1684, 1912, 3437, 3980]

In [65]:
run egos.py


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/Users/sukruhasdemir/anaconda/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    176             else:
    177                 filename = fname
--> 178             __builtin__.execfile(filename, *where)

/Users/sukruhasdemir/Dropbox/python-study/ipython-book/data/egos.py in <module>()
      4         folder = sys.argv[1]
      5 
----> 6 files = os.listdir(folder)
      7 
      8 ids = sorted(set(map(lambda file: int(file.split('.')[0]), files)))

NameError: name 'folder' is not defined

In [69]:
run -i egos.py

In [70]:
ids


Out[70]:
[0, 107, 348, 414, 686, 698, 1684, 1912, 3437, 3980]

NetworkX


In [2]:
import networkx as nx

In [9]:
g = nx.read_edgelist('0.edges')

In [10]:
len(g.nodes()), len(g.edges())


Out[10]:
(333, 2519)

In [77]:
nx.radius(g)


---------------------------------------------------------------------------
NetworkXError                             Traceback (most recent call last)
<ipython-input-77-73e1ad16c3de> in <module>()
----> 1 nx.radius(g)

/Users/sukruhasdemir/anaconda/lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyc in radius(G, e)
    140     """
    141     if e is None:
--> 142         e=eccentricity(G)
    143     return min(e.values())
    144 

/Users/sukruhasdemir/anaconda/lib/python2.7/site-packages/networkx/algorithms/distance_measures.pyc in eccentricity(G, v, sp)
     60         if L != order:
     61             msg = "Graph not connected: infinite path length"
---> 62             raise networkx.NetworkXError(msg)
     63 
     64         e[v]=max(length.values())

NetworkXError: Graph not connected: infinite path length

In [11]:
sg = nx.connected_component_subgraphs(g)

In [12]:
[len(s) for s in sg]


Out[12]:
[324, 3, 2, 2, 2]

In [13]:
sgfirst = sg[0]

In [14]:
nx.radius(sgfirst), nx.diameter(sgfirst)


Out[14]:
(6, 11)


In [85]:
class MyClass(object):
    def __dir__(self):
        return ['attr1', 'attr2']

In [86]:
obj = MyClass()

In [ ]:
obj.

In [89]:
??nx

In [94]:
%pfile egos.py


Object `egos.py` not found.


In [96]:
%timeit nx.center(sgfirst)


1 loops, best of 3: 209 ms per loop

In [97]:
nx.center(sgfirst)


Out[97]:
[u'51', u'190', u'83', u'307', u'175', u'237', u'277', u'124']

Profiling a script:


In [104]:
%run -t center.py


[u'51', u'190', u'83', u'307', u'175', u'237', u'277', u'124']

IPython CPU timings (estimated):
  User   :      64.32 s.
  System :       1.10 s.
Wall time:      64.67 s.

In [105]:
%run -p center.py


[u'51', u'190', u'83', u'307', u'175', u'237', u'277', u'124']


In [15]:
run -t center2.py


[u'51', u'190', u'83', u'307', u'175', u'237', u'277', u'124']

IPython CPU timings (estimated):
  User   :       0.46 s.
  System :       0.01 s.
Wall time:       0.46 s.


In [108]:
%%file text.txt
Hello world!


Overwriting text.txt

In [110]:
%%!
more text.txt


Out[110]:
['Hello world!']

In [122]:
code = """
import networkx as nx

g = nx.read_edgelist('0.edges')
sg = nx.connected_component_subgraphs(g)[0]

ecc = nx.eccentricity(sg)
r = nx.radius(sg)

center = [node for node in sg.nodes() if ecc[node] == r]

print center
"""
f = open('code2.py', 'w')
f.write(code)
f.close()

In [123]:
run code2.py


[u'51', u'190', u'83', u'307', u'175', u'237', u'277', u'124']

Hello World Example

In this example we'll learn how to:

  • Convert a cell in Markdown
  • Dispaly formatted text with Markdown


In [17]:
nx.draw_networkx(sgfirst, node_size=15, edge_color='b', with_labels=False, alpha=.4, linewidths=0)



In [ ]: