Download src/evolutionary_alrogithm.py from carterw/evolutionary-playlist-builder: direct link, hf CLI and curl.
- Browser
- Download file 15.9 kB
-
https://huggingface.co/spaces/carterw/evolutionary-playlist-builder/resolve/main/src/evolutionary_alrogithm.py
- Command line
-
hf download hf://spaces/carterw/evolutionary-playlist-builder/src/evolutionary_alrogithm.py
-
curl -L -o evolutionary_alrogithm.py https://huggingface.co/spaces/carterw/evolutionary-playlist-builder/resolve/main/src/evolutionary_alrogithm.py
15.9 kB
| import pandas as pd | |
| import numpy as np | |
| from scipy.spatial.distance import cosine | |
| import scikits.bootstrap as bootstrap | |
| import gradio as gr | |
| import plotly.express as px | |
| from plotly.graph_objects import Figure | |
| import matplotlib.pyplot as plt | |
| from copy import deepcopy | |
| from random import choice | |
| from typing import List, Set | |
| import itertools | |
| from sklearn.preprocessing import StandardScaler | |
| from src.spotipy_utils import SpotifyTrack | |
| # plt.switch_backend('Agg') # NOTE: for traversal direction tracking viz | |
| # Initial data cleaning. | |
| SONG_DF = pd.read_csv("data/SpotifyAudioFeaturesApril2019.csv") | |
| REDUCED_DF = pd.read_csv("data/reduced_audio_profiles.csv") | |
| AUDIO_DF = SONG_DF.drop(["artist_name", "track_id", "track_name", "duration_ms", "key", "mode", "time_signature", "popularity"], axis=1) # TODO: Normalize these feature-wise! | |
| AUDIO_FEATURE_NAMES = AUDIO_DF.columns | |
| history_df = pd.DataFrame({"x": REDUCED_DF.iloc[..., 0], | |
| "y": REDUCED_DF.iloc[..., 1], | |
| "status": ["none" for i in range(REDUCED_DF.shape[0])], | |
| "name": SONG_DF["track_name"].values, | |
| "artist": SONG_DF["artist_name"].values, | |
| "key": SONG_DF["key"].values}) | |
| # Symbols for track statuses. | |
| THUMBS_UP = "๐" | |
| THUMBS_DOWN = "๐" | |
| ADD_TO_PLAYLIST = "โ to โฏ๏ธ" | |
| COLOR_MAP_DISCRETE = {"none": 'grey', THUMBS_UP: "blue", THUMBS_DOWN: 'red', ADD_TO_PLAYLIST: "green"} | |
| # Normalizing audio profile features to be able to control mutation size more precisely. | |
| scaler = StandardScaler() | |
| audio_features = scaler.fit_transform(AUDIO_DF.values) | |
| sample_inds = list(np.arange(audio_features.shape[0])) # index list for easy sampling | |
| # Mutation and crossover options. | |
| MUTATION_OPTIONS = ["None", "Random", "Differential"] | |
| CROSSOVER_OPTIONS = ["None", "Two-Point"] | |
| class Individual: | |
| """ Individual representing a track in the Evolutionary algorithm population. | |
| """ | |
| def __init__(self, song_index:int=None, spotify_track=None): | |
| """Constructor for an Individual. | |
| Args: | |
| song_index (int): Index of the track in the dataset. | |
| """ | |
| if song_index: | |
| self.index = song_index | |
| self.spotify_id = SONG_DF.loc[song_index, ["track_id"]][0] | |
| self.spotify_track = SpotifyTrack(self.spotify_id) # Init a spotify track instance. | |
| self.name = SONG_DF.loc[song_index, ["track_name"]][0] | |
| self.artist = SONG_DF.loc[song_index, ["artist_name"]][0] | |
| self.genome = audio_features[song_index,...] # NOTE: For now, the only genome used is the audio profile of the track. | |
| elif spotify_track: | |
| self.index = None | |
| self.spotify_track = spotify_track | |
| self.name = self.spotify_track.get_name() | |
| self.artist = self.spotify_track.get_artist() | |
| self.genome = scaler.transform(self.spotify_track.get_track_audio_features().reshape(1, -1)).reshape(1, -1) | |
| self.status = None | |
| def get_image_url(self) -> str: | |
| """ Get the image for the track available on through the API. | |
| Returns: | |
| str: URL to the track image. | |
| """ | |
| return self.spotify_track.track_image | |
| def get_preview_url(self) -> str: | |
| """ Get the preview audio for the track available on through the API. | |
| Returns: | |
| str: URL to the preview audio. | |
| """ | |
| return self.spotify_track.track_preview_url | |
| def get_track_as_block(self) -> str: | |
| """Get track name and artist name in a string. | |
| Returns: | |
| str: Track name and artist name. | |
| """ | |
| return gr.TextArea(value=f"{self.name} by {self.artist}") | |
| def get_track_id(self) -> str: | |
| """Get track ID for Spotify API. | |
| Returns: | |
| str: Spotify track ID. | |
| """ | |
| return self.spotify_track.track_id | |
| class Population: | |
| """ Population of tracks to be evolved through interaction with a user in an effort to build a curated playlist. | |
| """ | |
| def __init__(self, num_songs:int, mutation_size:float, crossover_function=None, mutation_function=None): | |
| """ Constructor of the population initializing all the tracks in the first generation. | |
| Args: | |
| num_songs (int): Number of tracks in the population. | |
| mutation_size (float): Value to scale mutations. | |
| crossover_function (function, optional): Function to perform crossover with. Defaults to None. | |
| mutation_function (function, optional): Function to perform mutation with. Defaults to None. | |
| """ | |
| self.seed_track = None | |
| self.size = num_songs | |
| self.init_inds = np.random.choice(sample_inds, self.size) | |
| self.pop = [] | |
| self.search_results = None | |
| self.search_seed = None | |
| self.playlist_selected_songs = [] | |
| self.playlist_inds = set() | |
| self.user_key = None | |
| self.mutation_function = mutation_function | |
| self.crossover_function = crossover_function | |
| self.mutation_size = mutation_size | |
| self.history_df = deepcopy(history_df) | |
| self.distance_matrices = [] | |
| def mutate(self, thumbs_up:List[Individual], thumbs_down:List[Individual], added_songs:List[Individual]): | |
| """ Method to call assigned mutation function through. | |
| Args: | |
| thumbs_up (List[Individual]): List of individuals given the thumbs up. | |
| thumbs_down (List[Individual]): List of individuals given the thumbs down. | |
| added_songs (List[Individual]): List of individuals added to the playlist. | |
| """ | |
| self.mutation_function(self, thumbs_up, thumbs_down, added_songs) | |
| def crossover(self, thumbs_up:List[Individual], thumbs_down:List[Individual], added_songs:List[Individual]): | |
| """ Method to call assigned crossover function through. | |
| Args: | |
| thumbs_up (List[Individual]): List of individuals given the thumbs up. | |
| thumbs_down (List[Individual]): List of individuals given the thumbs down. | |
| added_songs (List[Individual]): List of individuals added to the playlist. | |
| """ | |
| self.crossover_function(self, thumbs_up, thumbs_down, added_songs) | |
| def add_to_playlist(self, track:Individual): | |
| """ Add a given track to the population's playlist. | |
| Args: | |
| track (Individual): Track that the user has chosen to add to the playlist. | |
| """ | |
| self.playlist_selected_songs.append(track) | |
| self.playlist_inds.add(track.index) | |
| def get_tracks_with_status(self, status:str): | |
| """ Get tracks in the current population matching some status. | |
| Args: | |
| status (str): Status to be matched on. | |
| """ | |
| return list(filter(lambda x: x.status == status, self.pop)) | |
| def get_playlist_block_value(self) -> str: | |
| """ Get population represented as a string. | |
| Returns: | |
| str: Tracks listed with name and artist name, one per line. | |
| """ | |
| val_string = "" | |
| for track in self.playlist_selected_songs: | |
| val_string += f"{track.name} by {track.artist}\n" | |
| return val_string | |
| def reinitialize_pop(self): | |
| """ Randomly re-initialize the population. | |
| """ | |
| self.init_inds = np.random.choice(sample_inds, self.size) | |
| self.pop = [Individual(ind) for ind in self.init_inds] | |
| def generate_traversal_viz(self) -> Figure: | |
| """ Generate a scatter plot marking the tracks which have been used so far and their categories. | |
| Returns: | |
| Figure: Plotly scatter plot. | |
| """ | |
| return px.scatter(self.history_df, "x", "y", color="status", color_discrete_map=COLOR_MAP_DISCRETE,labels={ | |
| "x": "", | |
| "y": "", | |
| },) | |
| def generate_direction_tracking(self): | |
| multidim_distance = np.stack(self.distance_matrices, axis=2) | |
| # Compute distribution of cosine similarity amongst distance vectors between individuals of the same generation. | |
| sibling_directional_similarity = np.zeros((multidim_distance.shape[2], multidim_distance.shape[0]*(multidim_distance.shape[0]-1))) | |
| for t in range(multidim_distance.shape[2]): | |
| cosine_similarities = [] | |
| for i in range(multidim_distance.shape[0]): | |
| for j in range(multidim_distance.shape[0]): | |
| if i == j: | |
| continue | |
| cosine_similarity = cosine(multidim_distance[i,:,t], multidim_distance[j,:,t]) | |
| cosine_similarities.append(cosine_similarity) | |
| sibling_directional_similarity[t, :] = np.array(cosine_similarities) | |
| num_obs = sibling_directional_similarity.shape[0] | |
| # Compute bootstrapped CI | |
| data_ci = [bootstrap.ci(sibling_directional_similarity[k, :]) for k in range(num_obs)] | |
| means = [ci.sum() / 2 for ci in data_ci] | |
| lower = [ci[0] for ci in data_ci] | |
| upper = [ci[1] for ci in data_ci] | |
| plt.plot(means, label = "Distribution of Cosine Similarity for Paternal Distance Vectors Among Siblings Over Time") | |
| plt.fill_between(np.arange(num_obs), upper, lower, alpha = 0.5) | |
| plt.savefig("cosine_dist_over_time.png") | |
| def update_population_history(self, thumbs_up:List[Individual], thumbs_down:List[Individual], added_songs:List[Individual]): | |
| """ Update the DF keeping track of the traversal history. | |
| Args: | |
| thumbs_up (List[Individual]): List of individuals given the thumbs up. | |
| thumbs_down (List[Individual]): List of individuals given the thumbs down. | |
| added_songs (List[Individual]): List of individuals added to the playlist. | |
| """ | |
| for track in thumbs_up: | |
| self.history_df.at[track.index, "status"] = THUMBS_UP | |
| for track in thumbs_down: | |
| self.history_df.at[track.index, "status"] = THUMBS_DOWN | |
| for track in added_songs: | |
| self.history_df.at[track.index, "status"] = ADD_TO_PLAYLIST | |
| def get_closest_song_to_genome(population: Population, genome: np.ndarray, new_track_inds: Set[int]) -> int: | |
| """ Find the song in the audio profile matrix that is most similar to the supplied one. | |
| Args: | |
| population (Population): Population of tracks. | |
| genome (np.ndarray): Vector of audio profile features. | |
| new_track_inds (Set[int]): Set of the track indices being added for the new generation. | |
| Returns: | |
| int: Index of the most similar track in the audio matrix that is not already in the population or added to the playlist. | |
| """ | |
| track_distances = np.linalg.norm(genome - audio_features, axis=1) # Compute euclidean distance from desired genome and all other audio profiles. | |
| sorted_distance_inds = np.argsort(track_distances) | |
| current_distance_index = 0 | |
| while sorted_distance_inds[current_distance_index] in new_track_inds or sorted_distance_inds[current_distance_index] in population.playlist_inds: | |
| current_distance_index += 1 | |
| child_index = sorted_distance_inds[current_distance_index] | |
| child = Individual(child_index) | |
| new_track_inds.add(child_index) | |
| return child | |
| def simple_crossover(population: Population, thumbs_up:List[Individual], thumbs_down:List[Individual], added_songs:List[Individual]): | |
| """ Performs simple two point crossover. | |
| Args: | |
| population (Population): Population to assign new children to. | |
| thumbs_up (List[Individual]): List of individuals given the thumbs up. | |
| thumbs_down (List[Individual]): List of individuals given the thumbs down. | |
| added_songs (List[Individual]): List of individuals added to the playlist. | |
| """ | |
| liked_tracks = list(itertools.chain(*[thumbs_up, added_songs])) | |
| if len(liked_tracks) < 2: # Not enough tracks to perform crossover. | |
| return | |
| new_track_inds = set() | |
| population_index = 0 | |
| while len(new_track_inds) < population.size: | |
| track_a, track_b = np.random.choice(liked_tracks, 2) | |
| crossover_point1, crossover_point2 = sorted(np.random.randint(0,len(track_a.genome),2)) | |
| track_a.genome[crossover_point1:crossover_point2+1] = track_b.genome[crossover_point1:crossover_point2+1] | |
| track_b.genome[crossover_point1:crossover_point2+1] = track_a.genome[crossover_point1:crossover_point2+1] | |
| child1 = get_closest_song_to_genome(population, track_a.genome, new_track_inds) | |
| population.pop[population_index] = child1 | |
| population_index += 1 | |
| if population_index < population.size: | |
| child2 = get_closest_song_to_genome(population, track_b.genome, new_track_inds) | |
| population.pop[population_index] = child2 | |
| population_index += 1 | |
| def simple_mutation(population: Population, thumbs_up:List[Individual], thumbs_down:List[Individual], added_songs:List[Individual]): | |
| """ Perform simple random mutation on a population of individuals. | |
| Args: | |
| population (Population): Population to assign new children to. | |
| thumbs_up (List[Individual]): List of individuals given the thumbs up. | |
| thumbs_down (List[Individual]): List of individuals given the thumbs down. | |
| added_songs (List[Individual]): List of individuals added to the playlist. | |
| """ | |
| liked_tracks = list(itertools.chain(*[thumbs_up, added_songs])) | |
| new_track_inds = set() | |
| # Init population distance matrix | |
| distance_matrix = np.zeros((population.size, population.size)) | |
| for i in range(population.size): | |
| liked_track = choice(liked_tracks) | |
| mutated_track_genome = liked_track.genome + np.random.uniform(size=(population.size))*population.mutation_size | |
| child = get_closest_song_to_genome(population, mutated_track_genome, new_track_inds) | |
| # Record distance vector for parent-child. | |
| distance_matrix[i, :] = child.genome - liked_track.genome | |
| if len(population.pop) <= i: | |
| population.pop.append(child) | |
| else: | |
| population.pop[i] = child | |
| # Add to distance matrix tracking list. | |
| population.distance_matrices.append(distance_matrix) | |
| def differential_mutation(population: Population, thumbs_up:List[Individual], thumbs_down:List[Individual], added_songs:List[Individual]): | |
| """ Perform differential mutation based on the user's decisions. | |
| Args: | |
| population (Population): Population to assign new children to. | |
| thumbs_up (List[Individual]): List of individuals given the thumbs up. | |
| thumbs_down (List[Individual]): List of individuals given the thumbs down. | |
| added_songs (List[Individual]): List of individuals added to the playlist. | |
| """ | |
| new_track_inds = set() | |
| # Init population distance matrix | |
| distance_matrix = np.zeros((population.size, population.size)) | |
| for i in range(population.size): | |
| if thumbs_down: | |
| disliked_track = choice(thumbs_down) | |
| else: | |
| disliked_track = Individual(np.random.choice(sample_inds, 1)[0]) | |
| if thumbs_up: | |
| seed_track = choice(thumbs_up) | |
| else: | |
| seed_track = Individual(np.random.choice(sample_inds, 1)[0]) | |
| if added_songs: | |
| attracting_track = choice(added_songs) | |
| elif population.playlist_selected_songs: | |
| attracting_track = choice(population.playlist_selected_songs) | |
| else: | |
| attracting_track = Individual(np.random.choice(sample_inds, 1)[0]) | |
| gene_distances = attracting_track.genome - disliked_track.genome | |
| resulting_genome = seed_track.genome + gene_distances*population.mutation_size | |
| child = get_closest_song_to_genome(population, resulting_genome, new_track_inds) | |
| # Record distance vector for parent-child. | |
| distance_matrix[i, :] = child.genome - attracting_track.genome | |
| population.pop[i] = child | |
| # Add to distance matrix tracking list. | |
| population.distance_matrices.append(distance_matrix) | |