def make_similarity_matrix(matrix, size=MIN_ALIGN): singles = matrix.tolist() points = [flatten(t) for t in tuples(singles, size)] numPoints = len(points) # euclidean distance distMat = np.sqrt(np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1, dtype=np.float32)) return distMat.reshape((numPoints, numPoints))
def infinite(graph, track, target): DG = nx.DiGraph() loops = get_jumps(graph, mode='backward') DG.add_edges_from(loops) DG, first_node, last_node = trim_graph(DG) def dist(node1, node2): return node2-node1 # search for shortest path from last to first node alt = True path = [] while path == []: edges = DG.edges(data=True) try: path = tuples(nx.astar_path(DG, last_node, first_node, dist)) except: if alt == True: DG.remove_node(first_node) alt = False else: DG.remove_node(last_node) alt = True DG, first_node, last_node = trim_graph(DG) assert(path != []) # FIXME -- maybe find a few loops and deal with them res = collect(edges, path) res_dur = 0 for r in res: if r[1] < r[0]: res_dur += r[2]['duration'] else: res_dur += r[1]-r[0] # trim graph to DG size f_n = min(graph.nodes()) while f_n < first_node: graph.remove_node(f_n) f_n = min(graph.nodes()) l_n = max(graph.nodes()) while last_node < l_n: graph.remove_node(l_n) l_n = max(graph.nodes()) # find optimal path path = compute_path(graph, max(target-res_dur, 0)) path = path + res # build actions actions = make_jumps(path, track) actions.pop(-1) jp = Jump(track, actions[-1].source, actions[-1].target, actions[-1].duration) actions.pop(-1) actions.append(jp) return actions
def do_work(audio_files, options): inter = float(options.inter) trans = float(options.transition) order = bool(options.order) equal = bool(options.equalize) verbose = bool(options.verbose) # Get pyechonest/remix objects analyze = lambda x : LocalAudioFile(x, verbose=verbose) tracks = map(analyze, audio_files) # decide on an initial order for those tracks if order == True: if verbose: print "Ordering tracks..." tracks = order_tracks(tracks) if equal == True: equalize_tracks(tracks) if verbose: print for track in tracks: print "Vol = %.0f%%\t%s" % (track.gain*100.0, track.analysis.pyechonest_track.title) print valid = [] # compute resampled and normalized matrices for track in tracks: if verbose: print "Resampling features for", track.analysis.pyechonest_track.title track.resampled = resample_features(track, rate='beats') track.resampled['matrix'] = timbre_whiten(track.resampled['matrix']) # remove tracks that are too small if is_valid(track, inter, trans): valid.append(track) # for compatibility, we make mono tracks stereo track = make_stereo(track) tracks = valid if len(tracks) < 1: return [] # Initial transition. Should contain 2 instructions: fadein, and playback. if verbose: print "Computing transitions..." start = initialize(tracks[0], inter, trans) # Middle transitions. Should each contain 2 instructions: crossmatch, playback. middle = [] [middle.extend(make_transition(t1, t2, inter, trans)) for (t1, t2) in tuples(tracks)] # Last chunk. Should contain 1 instruction: fadeout. end = terminate(tracks[-1], FADE_OUT) return start + middle + end
def cloud_dist(self,pointlist): ''' Returns the maximum of the distances of all pairs of points in pointlist. ''' dist=0 if len(pointlist)==1: return dist for t in tuples(2,pointlist): d=self.metric(t[0],t[1]) if d>=0: dist=max(dist,d) else: return -1 return dist
def from_clique_list(cls,wgraph,cliques,verify=False): ''' Input: a wGraph together with a list of simplices on the vertex set of the graph. It is assumed (and not checked unless verify==True) that the 1-skeleton of every simplex is contained in the graph. ''' simplices={0:[wSimplex([k],0) for k in wgraph._adj.keys()]} simplices[1]=[] for t in tuples(2,wgraph._adj.keys()): if wgraph.metric(t[0],t[1])>0: simplices[1].append(wSimplex(t,wgraph.metric(t[0],t[1]))) for v in cliques: if verify==True: ''' Check that the 1-skeleton of v is indeed contained in the wgraph. ''' for vlist in tuples(2,v): if wgraph.metric(vlist[0],vlist[1])==-1: raise ValueError('All cliques must have 1-skeleton included in wgraph.') for d in range(3,len(v)+1): for vlist in tuples(d,v): if not simplices.has_key(d-1): simplices[d-1]=[] weight=0 for i in range(d-1): inbrs=wgraph._adj[vlist[i]] for j in inbrs: if j[0] in vlist: weight=max(weight,j[1]) new_simplex=wSimplex(vlist,weight) if new_simplex not in simplices[d-1]: simplices[d-1].append(new_simplex) return wSimplicialComplex(wgraph,simplices)
def make_similarity_matrix_from_matrix(matrix): singles = matrix.tolist() points = [flatten(t) for t in tuples(singles, 1)] numPoints = len(points) distMat = sqrt(np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1, dtype=np.float32)) return distMat.reshape((numPoints, numPoints))
def compute_path(graph, target): first_node = min(graph.nodes()) last_node = max(graph.nodes()) # find the shortest direct path from first node to last node if target == 0: def dist(node1, node2): return node2-node1 # not sure why, but it works # we find actual jumps edges = graph.edges(data=True) path = tuples(nx.astar_path(graph, first_node, last_node, dist)) res = collect(edges, path) return res duration = last_node - first_node if target < duration: # build a list of sorted jumps by length. remaining = duration-target # build a list of sorted loops by length. loops = get_jumps(graph, mode='forward') def valid_jump(jump, jumps, duration): for j in jumps: if j[0] < jump[0] and jump[0] < j[1]: return False if j[0] < jump[1] and jump[1] < j[1]: return False if duration - (jump[1]-jump[0]+jump[2]['duration']) < 0: return False if duration - (jump[1]-jump[0]+jump[2]['duration']) < 0: return False return True res = [] while 0 < remaining: if len(loops) == 0: break for l in loops: if valid_jump(l, res, remaining) == True: res.append(l) remaining -= (l[1]-l[0]+l[2]['duration']) loops.remove(l) break if l == loops[-1]: loops.remove(l) break res = sorted(res, key=operator.itemgetter(0)) elif duration < target: remaining = target-duration loops = get_jumps(graph, mode='backward') tmp_loops = deepcopy(loops) res = [] # this resolution value is about the smallest denominator resolution = loops[-1][1]-loops[-1][0]-loops[-1][2]['duration'] while remaining > 0: if len(tmp_loops) == 0: tmp_loops = deepcopy(loops) d = -9999999999999999 i = 0 while d < resolution and i+1 <= len(tmp_loops): l = tmp_loops[i] d = remaining - (l[0]-l[1]+l[2]['duration']) i += 1 res.append(l) remaining -= (l[0]-l[1]+l[2]['duration']) tmp_loops.remove(l) order = np.argsort([l[0] for l in res]).tolist() res = [res[i] for i in order] else: return [(first_node, last_node)] def dist(node1, node2): return 0 # not sure why, but it works start = tuples(nx.astar_path(graph, first_node, res[0][0], dist)) end = tuples(nx.astar_path(graph, res[-1][1], last_node, dist)) return start + res + end
def make_similarity_matrix(matrix, size=MIN_ALIGN): singles = matrix.tolist() points = [flatten(t) for t in tuples(singles, size)] numPoints = len(points) distMat = sqrt(np.sum((repmat(points, numPoints, 1) - repeat(points, numPoints, axis=0))**2, axis=1, dtype=np.float32)) return distMat.reshape((numPoints, numPoints))