From: <gea...@us...> - 2009-04-30 17:26:18
|
Revision: 340 http://mypyspace.svn.sourceforge.net/mypyspace/?rev=340&view=rev Author: gearmonkey Date: 2009-04-30 17:26:17 +0000 (Thu, 30 Apr 2009) Log Message: ----------- I now have a highly unoptimized function that expands the graph to be songwise. My back of the napkin calculation says it will take 6 - 8 hours to expand the graph, which is annoyingly long and I expect it could be improved, but this will work for now. Modified Paths: -------------- graphRDF/branches/songsAsNodes/graphRDF.py Modified: graphRDF/branches/songsAsNodes/graphRDF.py =================================================================== --- graphRDF/branches/songsAsNodes/graphRDF.py 2009-04-30 15:01:37 UTC (rev 339) +++ graphRDF/branches/songsAsNodes/graphRDF.py 2009-04-30 17:26:17 UTC (rev 340) @@ -287,11 +287,40 @@ # self.AG.add_edge(node, friend) def populateSongwise(self): - '''Create a graph S which uses artist relationships as egdes but seperates each song into seperate nodes, such that song to song distance can be used as edge weights, rather than artist to artist distance. The artist as node graph must be built first, via either populate() or populateLocal().''' + '''Create a graph S which uses artist relationships as egdes but seperates each song into seperate nodes, such that song to song distance can be used as edge weights, rather than artist to artist distance. The artist as node graph must be built first, via either populate() or populateLocal(). On initalisation weights are set to 1.''' if not self.isPopulated: error("Base graph has not been built. Run populate() or populateLocal() first.") return self.S = igraph.Graph(directed=True) + idx = 0 + artistLookup = {} + for v in self.G.vs: + tracks = string2List(v["tracks"]) + if len(tracks) == 0: + print "no songs found for artist #" + str(v["uid"]) + " moving on." + artistLookup[v["uid"]] = [] + for track in tracks: + self.S.add_vertices(1) + self.S.vs[idx]['uid'] = v['uid'] + self.S.vs[idx]['track'] = str(track) + artistLookup[v["uid"]] += [idx] + idx += 1 + print str(self.S) + print "hope that's all the songs, now it's time to add some edges.\n----------\n----------" + idx = 0 + for index, edge in enumerate(self.G.es): + sources = artistLookup[self.G.vs[edge.source]['uid']] + targets = artistLookup[self.G.vs[edge.target]['uid']] + debug("Expand edge " + str(index) + " to " + str(len(sources) * len(targets)) + " edges.") + oldidx = idx + for source in sources: + for target in targets: + self.S.add_edges((source,target)) + self.S.es[idx]['audioWeight'] = -1 + idx += 1 + debug("added " + str(idx-oldidx) + " edges\n--------") + print "should have added all the edges now. Have a look:\n" + str(self.S) + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gea...@us...> - 2009-05-16 14:22:48
|
Revision: 346 http://mypyspace.svn.sourceforge.net/mypyspace/?rev=346&view=rev Author: gearmonkey Date: 2009-05-16 13:32:46 +0000 (Sat, 16 May 2009) Log Message: ----------- Added a function to access the path from src to dst, currently uweighted graphs only. Modified Paths: -------------- graphRDF/branches/songsAsNodes/graphRDF.py Modified: graphRDF/branches/songsAsNodes/graphRDF.py =================================================================== --- graphRDF/branches/songsAsNodes/graphRDF.py 2009-05-09 13:41:10 UTC (rev 345) +++ graphRDF/branches/songsAsNodes/graphRDF.py 2009-05-16 13:32:46 UTC (rev 346) @@ -388,9 +388,26 @@ + def shortestPath(self, src, dst, graph='S', weight='audioWeight'): + """returns the shortest path between the nodes src and dst (specified as node numbers) within graph (specify 'S' or 'G', S used by default). The edge attribute to use as a weight can be specified in weight, use None if an unweighted shortest path is desired. Looks to see if the shortest path index has been filled in self.graph.shortestpaths_unweighted or self.graph.shortestpaths_weighted (for audioWeight, exoitic weights not cached.)""" + #tests for existance of shortest paths for given src with the weighting and graph requested + if weight == None and graph = 'S': + try: + self.S.shortestpaths_unweighted[src] + except NameError: + self.S.shortestpaths_unweighted[src] = []*len(S.vs) + if not self.S.shortestpaths_unweighted[src][dst] == []: + return self.S.shortestpaths_unweighted[src][dst] + else: + self.S.shortestpaths_unweighted[src] = self.S.get_shortest_paths(src) + return self.S.shortestpaths_unweighted[src][dst] + else: + print "the specified edge attribute weight and graph were poorly specifed or are not yet supported..." + return + def getGenreAssortativity(self): '''get the assortivaty coeff for the igraph G - based on Newman 2002 "Mixing Patterns in Networks" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gea...@us...> - 2009-05-17 15:34:13
|
Revision: 347 http://mypyspace.svn.sourceforge.net/mypyspace/?rev=347&view=rev Author: gearmonkey Date: 2009-05-17 15:34:06 +0000 (Sun, 17 May 2009) Log Message: ----------- fixed the bugs and cleaned up the shortest path function. Should work with unweighted or audio weight graphs now. Modified Paths: -------------- graphRDF/branches/songsAsNodes/graphRDF.py Modified: graphRDF/branches/songsAsNodes/graphRDF.py =================================================================== --- graphRDF/branches/songsAsNodes/graphRDF.py 2009-05-16 13:32:46 UTC (rev 346) +++ graphRDF/branches/songsAsNodes/graphRDF.py 2009-05-17 15:34:06 UTC (rev 347) @@ -389,18 +389,36 @@ def shortestPath(self, src, dst, graph='S', weight='audioWeight'): - """returns the shortest path between the nodes src and dst (specified as node numbers) within graph (specify 'S' or 'G', S used by default). The edge attribute to use as a weight can be specified in weight, use None if an unweighted shortest path is desired. Looks to see if the shortest path index has been filled in self.graph.shortestpaths_unweighted or self.graph.shortestpaths_weighted (for audioWeight, exoitic weights not cached.)""" + """returns the shortest path between the nodes src and dst (specified as node numbers) within graph (specify 'S' or 'G', S used by default). The edge attribute to use as a weight can be specified in weight, use None if an unweighted shortest path is desired. Looks to see if the shortest path index has been filled in self.graph.shortestpaths_unweighted or self.graph.shortestpaths_weighted (for audioWeight, exotic weights not cached.)""" #tests for existance of shortest paths for given src with the weighting and graph requested - if weight == None and graph = 'S': + if weight == None and graph == 'S': try: - self.S.shortestpaths_unweighted[src] - except NameError: - self.S.shortestpaths_unweighted[src] = []*len(S.vs) - if not self.S.shortestpaths_unweighted[src][dst] == []: - return self.S.shortestpaths_unweighted[src][dst] - else: - self.S.shortestpaths_unweighted[src] = self.S.get_shortest_paths(src) - return self.S.shortestpaths_unweighted[src][dst] + self.S_shortestpaths_unweighted + except AttributeError: + self.S_shortestpaths_unweighted = [[]]*len(self.S.vs) + try: + return self.S_shortestpaths_unweighted[src][dst] + # try: + # if not self.S_shortestpaths_unweighted[src][dst] == []: + # return self.S_shortestpaths_unweighted[src][dst] + # else: + # self.S_shortestpaths_unweighted[src] = self.S.get_shortest_paths(src) + # return self.S_shortestpaths_unweighted[src][dst] + except IndexError: + self.S_shortestpaths_unweighted[src] = self.S.get_shortest_paths(src) + return self.S_shortestpaths_unweighted[src][dst] + + + elif weight == 'audioWeight' and graph == 'S': + try: + self.S_shortestpaths_audioWeighted + except AttributeError: + self.S_shortestpaths_audioWeighted = [[]]*len(self.S.vs) + try: + return self.S_shortestpaths_audioWeighted[src][dst] + except IndexError: + self.S_shortestpaths_audioWeighted[src] = self.S.get_shortest_paths(src,weight) + return self.S_shortestpaths_audioWeighted[src][dst] else: print "the specified edge attribute weight and graph were poorly specifed or are not yet supported..." return This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |