|
From: <gea...@us...> - 2009-05-26 21:16:01
|
Revision: 349
http://mypyspace.svn.sourceforge.net/mypyspace/?rev=349&view=rev
Author: gearmonkey
Date: 2009-05-26 21:15:29 +0000 (Tue, 26 May 2009)
Log Message:
-----------
added the weight loader.
Added Paths:
-----------
graphRDF/branches/songsAsNodes/loadWeights.py
Added: graphRDF/branches/songsAsNodes/loadWeights.py
===================================================================
--- graphRDF/branches/songsAsNodes/loadWeights.py (rev 0)
+++ graphRDF/branches/songsAsNodes/loadWeights.py 2009-05-26 21:15:29 UTC (rev 349)
@@ -0,0 +1,109 @@
+#!/usr/bin/env python
+# encoding: utf-8
+"""
+loadWeights.py
+
+reads in the tab delimited list from arg1 and a graph from arg2.
+For each line in arg1 checks that there are 3 values.
+Takes the first two values as the edge specification and the third as the the audioWeight.
+If the third value isn't present writes the edge to a text file named in arg3.
+Saves the updated graph in graphmlz formated file named in arg4.
+
+all args are required.
+
+Created by Benjamin Fields on 2009-05-17.
+Copyright (c) 2009 Goldsmiths University of London. All rights reserved.
+"""
+
+import sys
+import os
+import igraph
+import rightDict
+#import graphRDF
+
+Usage = """Usage:\nloadWeights.py weightfile.txt inGraph.mlz edgesLeft.txt outGraph.mlz\n\t\tFor more help read the source."""
+
+def createVerticesAttributeValueDict(workingGraph, attribute):
+ """creates a rightDict of the values in a graph of a given attribute and the node it came from to enable looser string matching (will return a substring match as true). May not work well if the values are non unique.
+ returns dictionary"""
+ attrValueDict = rightDict.rightDict()
+ for vert in workingGraph.vs:
+ attrValueDict[vert[attribute]] = vert.index
+ return attrValueDict
+
+def main(argv=None):
+ #if the graph has a different prefix than
+ prefix = "http://myrdfspace.com/media_seed_134901208/"
+
+ if argv is None:
+ argv = sys.argv
+ if len(argv) != 5:
+ print Usage
+ return 1
+
+ try:
+ infileHandle = open(argv[1], 'r')
+ outfileHandle = open(argv[3], 'w')
+ typoOutHandle = open('typos.txt', 'w')
+ except Exception, err:
+ print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err)
+ print >> sys.stderr, "\t" + Usage
+ return 2
+ print "loading graph into memory..."
+ try:
+ putWeightsInHere = igraph.Graph.Load('complexSongGraph.mlz', format='graphmlz')
+ print "again..."
+ putWeightsInHere = igraph.Graph.Load('complexSongGraph.mlz', format='graphmlz')
+ except RuntimeWarning:
+ print "Trying a second time..."
+ putWeightsInHere = igraph.Graph.Load('complexSongGraph.mlz', format='graphmlz')
+ except Exception, err:
+ print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err)
+ print >> sys.stderr, "\t" + Usage
+ return 3
+
+
+ missed = 0
+ typos = 0
+ foundWeight = 0
+ print "processing..."
+ indexdict = createVerticesAttributeValueDict(putWeightsInHere, 'track')
+ for idx, line in enumerate(infileHandle):
+
+ if idx%1000 == 0:
+ print str(idx) + " lines of " + argv[1] + " have been processed."
+
+ try:
+ sourceV , targetV, Weight = line.strip().split('\t')
+ except ValueError:
+ outfileHandle.write(str(idx) + " : Missing Weight : " + line)
+ outfileHandle.flush()
+ missed += 1
+ continue
+ try:
+ srcIDX = indexdict[sourceV]
+ trgtIDX = indexdict[targetV]
+
+ igraph.EdgeSeq(putWeightsInHere, [srcIDX, trgtIDX])[0]['audioWeight'] = float(Weight)
+ except Exception, err:
+ typoOutHandle.write(str(idx) + " : typo : " + line)
+ typoOutHandle.flush()
+ typos += 1
+ print >> sys.stderr, str(err) + "\n" + str(typos) + " typos found."
+ continue
+
+ foundWeight += 1
+
+ print "changed the weight of " + str(foundWeight) + " edges."
+ print "found " + str(missed) + " lines without weight."
+ print "Saving updated graph to " + str(argv[4])
+
+ putWeightsInHere.write(argv[4], format='graphmlz')
+ infileHandle.close()
+ outfileHandle.close()
+ typoOutHandle.close()
+
+
+if __name__ == '__main__':
+ sys.exit(main())
+
Property changes on: graphRDF/branches/songsAsNodes/loadWeights.py
___________________________________________________________________
Added: svn:executable
+ *
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|