Skip to content

Instantly share code, notes, and snippets.

@fajran
Created May 15, 2009 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fajran/112301 to your computer and use it in GitHub Desktop.
Save fajran/112301 to your computer and use it in GitHub Desktop.
Raw input data: snapshot-07.csv
- Contains one line header in the first line
Convert to tulip format:
$ sed '1d' snapshot-07.csv | ./group.py | ./to-tulip output.tlp
#!/usr/bin/env python
# Usage:
#
# $ chmod +x group.py
# $ sed '1d' snapshot-07.csv | ./group.py > data.txt
#
import sys
group = {}
for line in sys.stdin:
(author, reviewer, weight, g) = line.strip().split(",")
g = int(g)
weight = int(weight)
key = "%s,%s" % (author, reviewer)
gg = group.get(g, {})
total = gg.get(key, 0)
total += weight
gg[key] = total
group[g] = gg
for g in group:
for key in group[g]:
(author, reviewer) = map(lambda x: x[1:-1], key.split(","));
print "%s %s %d %d" % (author, reviewer, group[g][key], g)
TLP_HOME=/home/iang/apps/tulip-3.1.0
CXXFLAGS=-I$(TLP_HOME)/include
LIBS=-L$(TLP_HOME)/lib -ldl -ltulip
all : to-tulip
to-tulip : to-tulip.cc
g++ -o $@ $? $(CXXFLAGS) $(LIBS)
#include <tulip/Graph.h>
#include <tulip/StringProperty.h>
#include <tulip/IntegerProperty.h>
#include <iostream>
#include <fstream>
#include <list>
#include <set>
#include <map>
#define _E(s) std::cerr << s << std::endl;
#define _D(s) std::cout << s << std::endl;
#define EXISTS(c, v) (c).find(v) != (c).end()
#define FOREACH(c, i) for (i = (c).begin(); i != (c).end(); i++)
int main(int argc, char** argv)
{
if (argc < 2) {
_E("Usage: cat data.txt | ./to-tulip output.tlp");
return 1;
}
std::string author, reviewer;
std::string sweight, ssnapshot;
int weight, snapshot;
tlp::Graph* graph = tlp::newGraph();
typedef std::map<std::string, tlp::node> tPersonMap;
typedef std::set<std::string> tPersonList;
typedef std::pair<tlp::node, tlp::node> tCouple;
typedef std::pair<int, int> tData;
typedef std::pair<tCouple, tData> tRecord;
typedef std::list<tRecord> tRecords;
tPersonList person;
tPersonMap map;
tRecords records;
tlp::StringProperty* gName = graph->getProperty<tlp::StringProperty>("personName");
tlp::IntegerProperty* gWeight = graph->getProperty<tlp::IntegerProperty>("weight");
tlp::IntegerProperty* gSnapshot = graph->getProperty<tlp::IntegerProperty>("snapshot");
while (std::cin) {
std::cin >> author;
std::cin >> reviewer;
std::cin >> weight;
std::cin >> snapshot;
tlp::node nAuthor, nReviewer;
if (EXISTS(person, author)) {
nAuthor = map[author];
}
else {
person.insert(author);
nAuthor = graph->addNode();
map[author] = nAuthor;
gName->setNodeValue(nAuthor, author);
}
if (EXISTS(person, reviewer)) {
nReviewer = map[reviewer];
}
else {
person.insert(reviewer);
nReviewer = graph->addNode();
map[reviewer] = nReviewer;
gName->setNodeValue(nReviewer, reviewer);
}
tCouple couple(nAuthor, nReviewer);
tData data(weight, snapshot);
tRecord record(couple, data);
records.push_back(record);
}
tRecords::iterator iter;
FOREACH(records, iter) {
tRecord record = *iter;
tCouple couple = record.first;
tData data = record.second;
tlp::node nAuthor = couple.first;
tlp::node nReviewer = couple.second;
int weight = data.first;
int snapshot = data.second;
tlp::edge edge = graph->addEdge(nAuthor, nReviewer);
gWeight->setEdgeValue(edge, weight);
gSnapshot->setEdgeValue(edge, snapshot);
}
tlp::saveGraph(graph, argv[1]);
_E("Total nodes: " << graph->numberOfNodes());
_E("Total edges: " << graph->numberOfEdges());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment