{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":0: FutureWarning: IPython widgets are experimental and may change in the future.\n" ] } ], "source": [ "%run \"libraries.ipynb\"\n", "\n", "from IPython.display import display, HTML\n", "from IPython.html import widgets\n", "import matplotlib.patheffects as path_effects" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "hyperlinks_graph = nx.read_gexf(\"data/hyperlinks.gexf\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "g = nx.DiGraph()\n", "\n", "nb_out = 3\n", "\n", "hyperlinks_graph.remove_nodes_from([ \"Geometry\", \"Mathematics\" ])\n", "\n", "for u in hyperlinks_graph:\n", " nb = sorted(hyperlinks_graph[u].items(), key=lambda x: -x[1][\"term occurence\"])\n", " \n", " # filtering self loops\n", " nb = [ n for n in nb if n[0] != u ]\n", "\n", " for v in nb[0:nb_out]:\n", " #print v\n", " g.add_edge(u, v[0], attr_dict=v[1])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "reduced graph\n", "=============\n", "nodes: 296\n", "edges: 838\n", "reduction: 838/3469 (0.241568175267)\n" ] } ], "source": [ "print \"reduced graph\"\n", "print \"=============\"\n", "print \"nodes: %s\" % (len(g.nodes()))\n", "print \"edges: %s\" % (len(g.edges()))\n", "print \"reduction: %s/%s (%s)\" % (len(g.edges()),\n", " len(hyperlinks_graph.edges()),\n", " float(len(g.edges()))/float(len(hyperlinks_graph.edges())))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nx.write_gexf(g, \"data/reading_maps/pages-hyperlinks-reduced-3.gexf\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## community detection" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import community\n", "partitions = community.best_partition(g.to_undirected())" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "

group 0

Discrete geometry, Pi, Point in polygon, Convex hull, Star polygon, Wallpaper group, Polygon triangulation, Penrose tiling, Polygon, Euclidean shortest path, Prototile, Aperiodic tiling, Honeycomb (geometry), Crystal, Internal and external angle, Zonohedron, Regular Polytopes (book), Fractal, Polyhedron, Delaunay triangulation, Quasicrystal, Voronoi diagram, Wallace–Bolyai–Gerwien theorem, Uniform tessellation, Pattern, Wang tile, Dissection problem, Computational geometry, Tessellation, Hilbert's third problem, Schläfli symbol, Archimedean solid, Graham scan, Rhombus

group 1

Invariant (mathematics), Annulus (mathematics), Pappus's centroid theorem, Parametric surface, Birational geometry, Systolic geometry, Mathematics and fiber arts, Torus, Heronian triangle, Pythagorean triple, Heronian tetrahedron, Spline (mathematics)

group 2

Triangle inequality, Spherical trigonometry, Symmedian, Point (geometry), Squaring the circle, Isosceles trapezoid, Napkin ring problem, Equilateral triangle, List of circle topics, Distance geometry, Acute and obtuse triangles, Incircle and excircles of a triangle, Holditch's theorem, Cavalieri's principle, Tangential quadrilateral, Rectangle, Heron's formula, Circumscribed circle, Brahmagupta's formula, List of triangle inequalities, Ptolemy's theorem, Van Hiele model, Nine-point circle, Angle trisection, Inscribed angle, Dividing a circle into areas, Trigonometry, Quadrilateral, Kite (geometry), Orthodiagonal quadrilateral, Pons asinorum, Central angle, Altitude (triangle), Euclidean geometry, Equidiagonal quadrilateral, Trapezoid, Sphericon, Euclidean distance, Orthocentric system, Euler line, Right triangle, Isosceles triangle, Homothetic center, Integer triangle, Synthetic geometry, Pythagorean theorem, Reuleaux triangle, Bretschneider's formula, Pedal triangle, List of triangle topics, Parallelogram law, Thales' theorem, List of trigonometry topics, Angle, Triangle, Concyclic points, Bicentric quadrilateral, Cyclic quadrilateral, The Method of Mechanical Theorems, Parallel postulate, Point group

group 3

Shear mapping, Prismatoid, Kepler–Poinsot polyhedron, Kissing number problem, Regular polytope, Ehrhart polynomial, Polytope compound, Convex uniform honeycomb, Polytope, Deltahedron, N-sphere, Prism (geometry), Platonic solid, Dihedral angle, Normal (geometry), Angular defect, Uniform polyhedron, Four-dimensional space, Johnson solid, Pick's theorem, Tetrahedron, Hyperplane

group 4

Circle, Poncelet–Steiner theorem, Contact geometry, Golden angle, Square, Power center (geometry), Analytic geometry, Coordinate-free, Curve of constant width, Isoperimetric inequality, Pyramid (geometry), Shape, Straightedge, Mrs. Miniver's problem, Compass-and-straightedge construction, Geometric shape, Symplectic geometry

group 5

Dandelin spheres, Parametric equation, Semi-major axis, Soddy's hexlet, Similarity (geometry), Pascal's theorem, Hyperplane at infinity, Matrix representation of conic sections, Plane at infinity, Strähle construction, Hyperbola, Concurrent lines, Astronomy, Sangaku, Pappus's hexagon theorem, Parabola, Line at infinity, Pedoe's inequality, Locus (mathematics), Ellipse

group 6

Steiner chain, Klein geometry, Descriptive geometry, Riemann sphere, Inversive geometry, Minkowski space, Parabolic geometry (differential geometry), Projective geometry, Riemannian geometry, Information geometry, List of interactive geometry software, Cross-ratio, Projective line over a ring, Homothetic transformation, Projective line, Conformal geometry, Pseudosphere, Ruppeiner geometry, Affine transformation, Affine space, Affine geometry, Lie sphere geometry, 3D projection, Hyperbolic geometry, Arc (projective geometry), Möbius transformation, Differential geometry, Stereographic projection, Homography, Projective space

group 7

Elliptic geometry, Complex geometry, Geometrization conjecture, Enumerative geometry, Eccentricity (mathematics), Paraboloid, Cone, Focus (geometry), Parabolic reflector, Algebraic geometry, Group action, Kepler conjecture, Topology, Complex projective plane, Quadric, Borromean rings, Conic section, Finite geometry, Sphere, Spheroid, Oval (projective plane), Point at infinity, 3-sphere, Quantum geometry, Solid geometry, Parabolic microphone, Toric variety, Ellipsoid, Tropical geometry, Sphere packing, Projective plane, Spherical geometry, Noncommutative geometry, Hyperboloid

group 8

Bézier curve, 2D geometric model, 3D computer graphics, Duality (projective geometry), 2D computer graphics, Mathematical morphology, Digital geometry, Ray tracing (graphics), Non-uniform rational B-spline, Image analysis, Homogeneous coordinates, B-spline, Hidden line removal, Cross section (geometry), Binary space partitioning, Computer graphics, Minkowski addition, Constructive solid geometry, Point location

group 9

Absolute geometry, Ordered geometry, Girard Desargues, Incidence (geometry), Non-Euclidean geometry, Line (geometry), Desargues' theorem, Monge's theorem, Incidence geometry, Taxicab geometry, Ball (mathematics), Hilbert's axioms

group 10

Crystal system, Integral geometry, Polar sine, Geometry of numbers, Minkowski's theorem, Parallelepiped, Lattice (group), Rotation (mathematics), Symmetry group, Hadwiger's theorem, Convex geometry

group 11

Congruence (geometry), Point groups in three dimensions, Root system, Reflection (mathematics), Erlangen program, Leech lattice, Parallel (geometry), Space group, Relative direction, Infinitesimal transformation, Frieze group, Coordinate rotations and reflections, Isometry, Symmetry, Glide reflection, Roman surface, Coxeter group, Translation (geometry), Point groups in two dimensions, Translational symmetry, Mirror image, Handedness, Chirality (mathematics), Transformation geometry
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def print_groups(communities):\n", " html = \"\"\n", "\n", " for c, ps in communities.iteritems():\n", " html += \"\"\n", " \n", " html += \"

group %s

\" % (c)\n", " html += \", \".join(map(lambda x: u\"{0}\".format(x), ps))\n", " html += \"
\"\n", "\n", " display(HTML(html))\n", " \n", "communities = {}\n", "for k, v in partitions.iteritems():\n", " communities.setdefault(v, []).append(k)\n", "\n", "\n", "print_groups(communities)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## subgraphs explorer" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%config InlineBackend.figure_formats=['svg']\n", "\n", "def map_colors(g ,partitions):\n", " size = np.max( [ x[1] for x in partitions.iteritems() ])\n", " \n", " for n in partitions:\n", " g.node[n][\"color\"] = float(partitions[n])/float(size)\n", "\n", "\n", "map_colors(g, partitions)\n", "\n", "pages = g.nodes()\n", "pages = sorted(pages)\n", "\n", "color_map = plt.get_cmap(\"Set2\")\n", "# color_map = sns.color_palette(\"Set2\", 11)\n", "\n", "print color_map\n", "def display_local_graph(page):\n", " nbunch = [ page ]\n", " nbunch.extend( list(g.to_undirected()[page]))\n", " g_sub = g.subgraph(nbunch)\n", "\n", " #nx.draw_spring(g2)\n", " pos = nx.spring_layout(g_sub,iterations=150)\n", " \n", " plt.figure(figsize=(6,6))\n", "\n", " colors = map(lambda x: g.node[x][\"color\"], g_sub.nodes())\n", " \n", " # draw nodes\n", " nodes = nx.draw_networkx_nodes(g_sub, pos, alpha=0.9, node_size=500, node_color=colors, cmap=color_map, vmin=0, vmax=1)\n", " nodes.set_edgecolor('w')\n", "\n", " # draw edges\n", " nx.draw_networkx_edges(g_sub, pos, alpha=0.3, width=1)\n", " \n", " # draw labels\n", " labels = nx.draw_networkx_labels(g_sub, pos, font_size=8, font_family=\"Bitstream Vera Sans\")\n", " map(lambda x: labels[x].set_path_effects([path_effects.Stroke(linewidth=1, foreground='white'),\n", " path_effects.Normal()]),labels)\n", " \n", " plt.axis('off')\n", " plt.show()\n", "\n", "\n", "w = widgets.Select(description=\"page\", options=pages)\n", "widgets.interact(display_local_graph, page=w);" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }