Last active
November 21, 2023 12:59
-
-
Save msarahan/f425082c67bd985cb4edd0edb4d50de0 to your computer and use it in GitHub Desktop.
graphs from conda repodata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"adapted from https://gist.github.com/ericdill/9942ac55c2c9f6a550973dd2dc3653a4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import copy\n", | |
"import json\n", | |
"import glob\n", | |
"from os.path import join, basename, isfile\n", | |
"from matplotlib import pyplot as plt\n", | |
"import networkx as nx\n", | |
"%matplotlib inline\n", | |
"\n", | |
"if not isfile('repodata.json'):\n", | |
" from six.moves import urllib\n", | |
" urllib.request.urlretrieve(\"https://repo.anaconda.com/pkgs/main/linux-64/repodata.json\", \"repodata.json\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dg = nx.DiGraph()\n", | |
"\n", | |
"for rec, meta in json.load(open('repodata.json'))['packages'].items():\n", | |
" dg.add_node(meta['name'])\n", | |
" for dep in meta.get('depends', []):\n", | |
" dg.add_edge(meta['name'], dep.split(' ')[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dg.number_of_nodes()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nx.draw_networkx(dg, pos, node_color='#A0CBE2', edge_color='#FFCBE2', with_labels=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def neighbor_subgraph(G, center_node, steps):\n", | |
" neighbors = set([center_node])\n", | |
" for n in range(steps):\n", | |
" immediate_neighbors = copy.copy(neighbors)\n", | |
" for neighbor in immediate_neighbors:\n", | |
" neighbors.update(nx.all_neighbors(G, neighbor))\n", | |
" return nx.subgraph(G, neighbors)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sg = neighbor_subgraph(dg, 'scikit-learn', 1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nx.draw_kamada_kawai(sg, with_labels=True, node_color='#A0CBE2', edge_color='#FFCBE2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nx.draw_shell(sg, with_labels=True, node_color='#A0CBE2', edge_color='#FFCBE2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nx.draw_random(sg, with_labels=True, node_color='#A0CBE2', edge_color='#FFCBE2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nx.draw_spring(sg, with_labels=True, node_color='#A0CBE2', edge_color='#FFCBE2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"nx.draw_circular(sg, with_labels=True, node_color='#A0CBE2', edge_color='#FFCBE2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# https://ocefpaf.github.io/python4oceanographers/blog/2014/11/17/networkX/\n", | |
"def trim_nodes(G, d):\n", | |
" \"\"\"Returns a copy of G without the nodes with a degree less than d.\n", | |
" http://glowingpython.blogspot.com/2012/11/first-steps-with-networx.html\n", | |
" \"\"\"\n", | |
" Gt = G.copy()\n", | |
" dn = nx.degree(Gt)\n", | |
" for n in G.nodes():\n", | |
" if dn[n] <= d:\n", | |
" Gt.remove_node(n)\n", | |
" return Gt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"trim_graph = trim_nodes(dg, 20)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pos = nx.shell_layout(trim_graph)\n", | |
"nx.draw_networkx(trim_graph, pos, node_color='#A0CBE2', edge_color='#FFCBE2', with_labels=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
For those not wanting to fire up notebooks to check dependencies, I wrote a CLI utility that more or less does the same thing: https://github.com/omegacen/conda-depgraph.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example, @msarahan! What is
pos
in the 4th cell?