Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168738
Image: ubuntu2004

Let's try to make a graph of the dependencies among spkgs in Sage!

We first make some imports explicit and note the version of Sage for which we're making this graph:

import os, subprocess from sage.version import version from sage.misc.misc import SAGE_ROOT version
'4.1'

Appropriately enough, the relevant "Makefile" in this case lives under spkg directory:

fd = open(os.path.join(SAGE_ROOT, 'spkg/standard/deps'), 'r') deps = fd.read() fd.close()

Now we generate a dictionary whose keys are spkgs.  The corresponding value for each key is a list of prerequisite spkgs, distilled from the Makefile.  (Note: It may be better to use regular expressions instead.)  We also prepare a short list of basic dependencies which we can later exclude from the graph.

d = deps.lower().replace('\\\n', '').replace('\t', '').replace('_spkg','') d = d.replace('$(inst)/', '').replace('$(', '').replace(')', '') targets = [li for li in d.split('\n') if ':' in li and '#' not in li] spkg = {} for t in targets: t = t.split(':') spkg[t[0]] = t[1].split(None) del spkg['base'] common = ['base', 'dir', 'prereq', 'sage_bzip2']

With the spkg dictionary we construct a directed graph, which is a graph whose edges can be "arrows."  For convenience, we reverse all of the directed edges and delete some vertices.  For more on graphs in Sage, please see the reference manual.  Finally, we try to view the graph, er, graphically:

g = DiGraph(spkg).reverse() g.delete_vertices(common) g.plot()

That is quite messy, but with some insight from Robert Miller, we can get a neater layout.  First, we verify that our graph is indeed a directed acyclic graph (DAG):

g.is_directed_acyclic()
True

This means that none of our spkgs is a prerequisite for itself, either directly or indirectly, which is reassuring news.  With this information we can construct a partially ordered set, or poset, from our graph and plot it instead:

p = Poset(g) p.plot(vertex_size=1500, vertex_colors='#ccffcc').show(figsize=35, aspect_ratio=4)

We can get an another representation if the Graphviz package is installed locally:

loc = '' format = 'png' gv_base = os.path.join(loc, 'spkg_deps-' + version) gv_dot = gv_base + '.dot' g.graphviz_to_file_named(gv_dot) cmd = 'dot ' + gv_dot + ' -T' + format + ' -o ' + gv_base + '.' + format + ' ' opts = '-Gratio=auto -Gsize=100,10' ret = subprocess.call(cmd + opts, shell=True)
/bin/sh: dot: not found

It's not, apparently, but here's an uploaded image:

html('<img src="spkg_deps-4.1.png" />')

This is just a start.  Happy graphing!