def do_random_path(self, line): ''' picks two artists at random and finds a path between them''' count = 1 if len(line) > 0: count = int(line) paths = 0 no_path = 0 sum_time = 0 sum_length = 0 max_length = 0 max_time = 0 for i in xrange(count): aid1 = db.artist_random() aid2 = db.artist_random() print "random path between", aid1, db.artist_name(aid1), 'and', aid2, db.artist_name(aid2) start = time.time() path = db.artist_path(aid1, aid2, skipset=self.skipset) delta = time.time() - start if path: self.print_path(path, 0) paths += 1.0 lpath = len(path['links']) sum_length += lpath sum_time += delta if lpath > max_length: max_length = lpath if delta > max_time: max_time = delta else: print "no path between", aid1, db.artist_name(aid1), aid2, db.artist_name(aid2) no_path += 1 print print 'paths:', paths print 'no paths:', no_path if paths > 0: avg_time = sum_time / paths avg_length = sum_length / paths print 'avg length:', avg_length print 'avg time:', avg_time print 'max length:', max_length print 'max time:', max_time
def do_find_popular_crossroads(self, line): ''' creates random paths and find the most common crossroads ''' nodes = collections.defaultdict(int) count = 100 if len(line) > 0: count = int(line) paths = 0 no_path = 0 sum_time = 0 sum_length = 0 sum_score = 0 max_length = 0 max_time = 0 max_score = 0 avg_score = 0 for i in xrange(count): aid1 = db.artist_random() aid2 = db.artist_random() start = time.time() score, aids = db.artist_raw_path(aid1, aid2, skipset=self.skipset) delta = time.time() - start if aids: for aid in aids: nodes[aid] += 1 paths += 1.0 lpath = len(aids) sum_score += score if score > max_score: max_score = score sum_length += lpath sum_time += delta if lpath > max_length: max_length = lpath if delta > max_time: max_time = delta else: print "no path between", aid1, db.artist_name(aid1), aid2, db.artist_name(aid2) no_path += 1 nlist = [ (v,k) for k,v in nodes.items()] nlist.sort(reverse=True) print for count, aid in nlist[:40]: print count, aid, db.artist_name(aid) print print 'paths:', paths print 'no paths:', no_path if paths > 0: avg_time = sum_time / paths avg_length = sum_length / paths avg_score = sum_score / paths print 'avg length:', avg_length print 'avg time:', avg_time print 'avg score:', avg_score print 'max length:', max_length print 'max time:', max_time print 'max score:', max_score