{"title":"Joe Thomas - Python, Projects","link":[{"@attributes":{"href":"https:\/\/jsthomas.github.io\/","rel":"alternate"}},{"@attributes":{"href":"https:\/\/jsthomas.github.io\/feeds\/python-projects.atom.xml","rel":"self"}}],"id":"https:\/\/jsthomas.github.io\/","updated":"2014-04-02T00:00:00-04:00","entry":{"title":"A Mathematics Geneaology Project Visualizer","link":{"@attributes":{"href":"https:\/\/jsthomas.github.io\/math-genealogy-visualizer.html","rel":"alternate"}},"published":"2014-04-02T00:00:00-04:00","updated":"2014-04-02T00:00:00-04:00","author":{"name":"Joe Thomas"},"id":"tag:jsthomas.github.io,2014-04-02:\/math-genealogy-visualizer.html","summary":"<p>A program to visualize data from the Mathematics Geneaology Project<\/p>","content":"<p>The\n<a href=\"http:\/\/genealogy.math.ndsu.nodak.edu\/\">Mathematics Geneaology Project<\/a>\n(MGP) is a publicly searchable database that records advisor-advisee\nrelations between mathematicians. Their website is interesting,\nparticularly if you're interested in the history of\nmathematics. Unfortunately, their website is also mostly text-based;\nthere isn't a way to visualize the relationships between large groups\nof people.<\/p>\n<p>I've created a tool that produces a directed graph displaying the\nadvisor-advisee relations stored in the MGP database. I created this\ntool in 2012 in order to learn about how to process data from the web\nusing python.<\/p>\n<h2>Example Output<\/h2>\n<p>After reading the steps below, you should be able to produce a picture\nlike the one below.<\/p>\n<p><img alt=\"A genealogical tree\" src=\"https:\/\/jsthomas.github.io\/images\/mgptree\/gauss.png\"><\/p>\n<p>Naturally, the real purpose of the tool is to make much bigger graphs\nthan this!<\/p>\n<h2>Download<\/h2>\n<p>You will need the following:<\/p>\n<ol>\n<li>\n<p>A copy of my python script which you can <a href=\"https:\/\/github.com\/jsthomas\/mgptree\">download here<\/a>.<\/p>\n<\/li>\n<li>\n<p>An installation of <a href=\"http:\/\/www.graphviz.org\/\">graphviz<\/a> (this\nprogram takes care of the actual graph drawing).<\/p>\n<\/li>\n<li>\n<p>Basic knowledge of the command line.<\/p>\n<\/li>\n<\/ol>\n<p>On an Ubuntu machine, these dependencies (<code>graphviz<\/code> and <code>requests<\/code>)\ncan be installed via:<\/p>\n<div class=\"highlight\"><pre><span><\/span><code>$ sudo apt install virtualenv graphviz\n$ virtualenv venv\n$ venv\/bin\/activate\n<span class=\"o\">(<\/span>venv<span class=\"o\">)<\/span> $ pip install requirements.txt\n<\/code><\/pre><\/div>\n\n<h2>Usage Example<\/h2>\n<p>The easiest way to learn how to use this tool is by way of an\nexample. Place the <code>mgptree<\/code> script in a convenient folder (the\nprogram generates a number of files). Change the permissions on the\nscript so that you can execute it.<\/p>\n<p>Let's say we want to generate a tree for Gau\u00df and Fourier, going back\n5 generations. The first step is to prepare a file of names for our\nsearch:<\/p>\n<p>So, we create a unicode text file:<\/p>\n<div class=\"highlight\"><pre><span><\/span><code>Gau\u00df, Carl, Friedrich\nFourier, Jean-Baptiste, Joseph\n<\/code><\/pre><\/div>\n\n<p>I'll call the file <code>gf.txt<\/code>. Now we need to get some data from the\nweb. We do this as follows:<\/p>\n<div class=\"highlight\"><pre><span><\/span><code>(venv) $ python mgptree.py -g 5 -s -i gf.txt -o gf.mgp\nSaved 25 records to file gf.mgp.\n<\/code><\/pre><\/div>\n\n<p>The <code>-s<\/code> option indicates we want to \"scrape\" data from the MGP\nwebsite. The <code>-g<\/code> option indicates how many generations backward we\nwant to search. The <code>-i<\/code> denotes our input file, the <code>-o gf.mgp<\/code>\nindicates that we want to save our results in a database named\n<code>gf.mgp<\/code>.<\/p>\n<p>Now we need to graph the data, so we type the following:<\/p>\n<div class=\"highlight\"><pre><span><\/span><code>(venv) $ python mgptree.py -p -i gf.mgp -o gf.dot\n(venv) $ dot -Tpng gf.dot -o gf.png\n<\/code><\/pre><\/div>\n\n<p>In the first line, the <code>-p<\/code> indicates we are now in \"plotting mode\"\nwhere we want to convert the input database <code>gf.mgp<\/code> to a textual\nformat, which we'll save as <code>gf.dot<\/code>. The next line hands the textual\ndata to the graphviz program <code>dot<\/code>, which converts it to a PNG image\nfile named <code>gf.png<\/code>. (Many other file formats are available.)<\/p>\n<p><img alt=\"Genealogical trees of Gauss and Fourier\" src=\"https:\/\/jsthomas.github.io\/images\/mgptree\/gf.png\"><\/p>\n<p>Creating the intermediate database <code>gf.mgp<\/code> allows us to work with the\ndata offline, without having to query the web while we are figuring\nout how we want to plot the data.  For example, suppose we only want\nto present two generations of the data we just downloaded. Then we'd\ntype<\/p>\n<div class=\"highlight\"><pre><span><\/span><code>(venv) $ python mgptree.py -p -g2 -i gf.mgp -o gf_tiny.dot\n(venv) $ dot -Tpng gf_tiny.dot -o gf_tiny.png\n<\/code><\/pre><\/div>\n\n<p>and we wouldn't have to retrieve any information from the web. The\nresult, <code>gf_tiny.png<\/code> looks like this:<\/p>\n<p><img alt=\"A small genealogical tree of Gauss and Fourier.\" src=\"https:\/\/jsthomas.github.io\/images\/mgptree\/gf_tiny.png\"><\/p>\n<p>This feature can be pretty useful if you're trying to work with very\nlarge sets of data and don't know how much information you want to\npresent.<\/p>\n<h2>Usage Tips<\/h2>\n<ol>\n<li>\n<p>The Math Geneaology Project has some \"holes\" in its data. This seems\nto be particularly true for mathematicians who didn't get their degree\nin Europe or the US. If you or your advisor aren't listed, add your\ndata to their website\n<a href=\"http:\/\/genealogy.math.ndsu.nodak.edu\/submit.php\">here<\/a> to help fill\nin the gaps!<\/p>\n<\/li>\n<li>\n<p>The number of requests to the MGP's database can grow to be quite\nlarge if you search back 10 or more generations. In particular, it's\nprobably not a good idea to try to generate a tree for a large number\nof people <em>and<\/em> many generations.<\/p>\n<\/li>\n<li>\n<p>Accurately matching name data isn't easy. For example, the\nmathematician Felix Klein is really named Christian Felix Klein. If\nyour searches don't succeed, check to make sure you've got your name\ndata right. This is particularly true for names with accents and\numlauts.<\/p>\n<\/li>\n<\/ol>\n<h2>Making Pretty Graphs<\/h2>\n<p>Because my script produces an intermediate <code>.dot<\/code> file, it is possible\nfor you to edit that file to add additional information or decorations\nto the graph. Don't like the font I chose? Change the <code>font=Courier<\/code>\nentry in the <code>.dot<\/code> file. Want to adjust a few nodes so that\nsignificant mathematicians have special node colors? Search for their\nnames in the <code>.dot<\/code> file, then add the metadata\n<code>color=[your color here]<\/code> to their nodes. The documentation for\ngraphviz is very useful for tasks like this.<\/p>","category":{"@attributes":{"term":"Python, Projects"}}}}