|
From: <gea...@us...> - 2009-05-27 21:16:43
|
Revision: 352
http://mypyspace.svn.sourceforge.net/mypyspace/?rev=352&view=rev
Author: gearmonkey
Date: 2009-05-27 21:16:28 +0000 (Wed, 27 May 2009)
Log Message:
-----------
Cleaned up the IO to do some autoformat detection.
Modified Paths:
--------------
graphRDF/branches/songsAsNodes/loadWeights.py
Modified: graphRDF/branches/songsAsNodes/loadWeights.py
===================================================================
--- graphRDF/branches/songsAsNodes/loadWeights.py 2009-05-26 21:29:16 UTC (rev 351)
+++ graphRDF/branches/songsAsNodes/loadWeights.py 2009-05-27 21:16:28 UTC (rev 352)
@@ -17,8 +17,10 @@
import sys
import os
+import os.path
import igraph
import rightDict
+import cPickle
#import graphRDF
Usage = """Usage:\nloadWeights.py weightfile.txt inGraph.mlz edgesLeft.txt outGraph.mlz\n\t\tFor more help read the source."""
@@ -51,12 +53,18 @@
return 2
print "loading graph into memory..."
try:
- putWeightsInHere = igraph.Graph.Load(argv[2], format='graphmlz')
- print "again..."
- putWeightsInHere = igraph.Graph.Load(argv[2], format='graphmlz')
- except RuntimeWarning:
- print "Trying a second time..."
- putWeightsInHere = igraph.Graph.Load(argv[2], format='graphmlz')
+ if os.path.splitext(argv[2])[1] in ['.pkl', '.pickle']:
+ print 'unpickling...'
+ putWeightsInHere = cPickle.load(open(argv[2], 'r'))
+ elif os.path.splitext(argv[2])[1] in ['.mlz', '.graphmlz']:
+ print 'rendering from graphmlz...'
+ putWeightsInHere = igraph.Graph.Load(argv[2], format='graphmlz')
+ else:
+ print 'pushing to igraph loader without a format (attempting auto detection)...'
+ putWeightsInHere = igraph.Graph.Load(argv[2])
+ # except RuntimeWarning:
+ # print "Trying a second time..."
+ # putWeightsInHere = igraph.Graph.Load(argv[2], format='graphmlz')
except Exception, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err)
print >> sys.stderr, "\t" + Usage
@@ -76,19 +84,26 @@
try:
sourceV , targetV, Weight = line.strip().split('\t')
except ValueError:
- outfileHandle.write(str(idx) + " : Missing Weight : " + line)
- outfileHandle.flush()
- missed += 1
- continue
+ outfileHandle.write(str(idx) + " : Missing Weight : " + line)
+ outfileHandle.flush()
+ missed += 1
+ print >> sys.stderr, str(missed) + " missing weights."
+ continue
try:
- srcIDX = indexdict[sourceV]
- trgtIDX = indexdict[targetV]
-
+ srcIDX = indexdict[sourceV.lstrip(prefix)]
+ trgtIDX = indexdict[targetV.lstrip(prefix)]
+
igraph.EdgeSeq(putWeightsInHere, [srcIDX, trgtIDX])[0]['audioWeight'] = float(Weight)
+ except ValueError:
+ outfileHandle.write(str(idx) + " : Missing Edge : " + line)
+ outfileHandle.flush()
+ typos += 1
+ print >> sys.stderr, str(typos) + " typos found."
+ continue
except Exception, err:
- typoOutHandle.write(str(idx) + " : typo : " + line)
- typoOutHandle.flush()
- typos += 1
+ typoOutHandle.write(str(idx) + " : typo : " + line)
+ typoOutHandle.flush()
+ typos += 1
print >> sys.stderr, str(err) + "\n" + str(typos) + " typos found."
continue
@@ -98,7 +113,16 @@
print "found " + str(missed) + " lines without weight."
print "Saving updated graph to " + str(argv[4])
- putWeightsInHere.write(argv[4], format='graphmlz')
+ if os.path.splitext(argv[4])[1] in ['.pkl', '.pickle']:
+ cPickle.dump(putWeightsInHere, open(argv[4], 'w'))
+ elif os.path.splitext(argv[4])[1] in ['.mlz', '.graphmlz']:
+ putWeightsInHere.write(argv[4], format='graphmlz')
+ else:
+ try:
+ putWeightsInHere.write(argv[4])
+ except IOError:
+ print "unable to determine the desired file format from the extension for the output graph.\nWriting it out as a graphmlz file and appending .mlz"
+ putWeightsInHere.write(argv[4]+'.mlz', format='graphmlz')
infileHandle.close()
outfileHandle.close()
typoOutHandle.close()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|