<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ben Atkin&#039;s Self-Hosted Blog &#187; graphs</title>
	<atom:link href="http://benatkin.com/tag/graphs/feed/" rel="self" type="application/rss+xml" />
	<link>http://benatkin.com</link>
	<description>My true voice on the Internet.</description>
	<lastBuildDate>Wed, 23 May 2012 13:55:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Graphing retweets with Python and GraphViz</title>
		<link>http://benatkin.com/2009/04/27/graphing-retweets-with-python-and-graphviz/</link>
		<comments>http://benatkin.com/2009/04/27/graphing-retweets-with-python-and-graphviz/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 06:37:09 +0000</pubDate>
		<dc:creator>Ben Atkin</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://benatkin.com/weblog/?p=308</guid>
		<description><![CDATA[On the microblogging site twitter, a blog post is called an update, or informally, a tweet. When someone copies a tweet and posts it to their twitter feed, it&#8217;s called a retweet. Sometimes opinions are retweeted by those who share &#8230; <a href="http://benatkin.com/2009/04/27/graphing-retweets-with-python-and-graphviz/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On the microblogging site twitter, a blog post is called an update, or informally, a tweet. When someone copies a tweet and posts it to their twitter feed, it&#8217;s called a retweet. Sometimes opinions are retweeted by those who share them, other times it&#8217;s information, and other times it&#8217;s silly memes. This evening, a local friend of mine on twitter started a meme by saying &#8220;tweet.&#8221; and asking for a retweet. One person who retweeted it got his message retweeted.</p>
<p><img src="http://benatkin.com/weblog/wp-content/uploads/2009/04/picture-5.png" alt="retweets" title="retweets" width="474" height="316" class="alignnone size-full wp-image-309"></p>
<p>I know I&#8217;m not the first person to graph retweets, but being curious as to what this particular graph would look like, I decided to do a graph in Python as a programming exercise. I did it using a GraphViz library. The code is here:</p>
<pre class="prettyprint" style="border: none">
import pydot

graph = pydot.Dot('rt', graph_type='digraph')
tweeps = ('tysoncrosbie',
           ('phxreguy',
             ('sbowerman',
               ('phxwebguy', 'leaky_tiki'),
               'refriedchicken')),
           'vhgill',
           'Yartibise')

def add_edge(source, dest):
  graph.add_edge(pydot.Edge(source, dest))

def first_flat(tree):
  if isinstance(tree, tuple):
    return first_flat(tree[0])
  else:
    return tree

def find_edges(tree):
  if isinstance(tree, tuple):
    source = tree[0]
    for dest in tree[1:]:
      add_edge(source, first_flat(dest))
      find_edges(dest)

find_edges(tweeps)
graph.write_png('rt_graph.png')
</pre>
<p>The code takes a nested list structure (a tree) and produces edges from it, which can be graphed by GraphViz. It uses pydot, a GraphViz library for Python. Here is the resulting image:</p>
<p><img src="http://benatkin.com/weblog/wp-content/uploads/2009/04/rt_graph1.png" alt="rt_graph1" title="rt_graph1" width="511" height="443" class="alignnone size-full wp-image-320"></p>
<p>Observations:</p>
<ul>
<li>
<b>Python doesn&#8217;t have a built-in list flattening function.</b> This was irritating. Ruby&#8217;s is Array#flatten. It would have been so much nicer to have been able to grab the first element of a flattened list rather than write the first_flat function or copy/paste a list-flattening function from the Internet.</li>
<li>
<b>pydot is really simple to use.</b> I liked how it could produce a png file. I was planning just to have it create a dot file and then use GraphViz to create a png, but when I saw the write_png function I decided to just use that.</li>
<li>
<b>GraphViz has reasonable defaults.</b> It produced a nice-looking graph on the first try. I think that&#8217;s a big part of why GraphViz is as popular as it is.</li>
</ul>
<p>I was hoping to have a little more to show for tomorrow&#8217;s Python Interest Group meeting, but this will have to do!</p>
]]></content:encoded>
			<wfw:commentRss>http://benatkin.com/2009/04/27/graphing-retweets-with-python-and-graphviz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

