From 46df571855b765c926c3e59c08f7cc13f3d01829 Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Tue, 22 May 2018 20:25:29 -0400 Subject: [PATCH] Compute the average and std_dev for popularity Modified the structure and code in utils.py so that library_stats now has a record of the mean and standard deviation of popularity. --- spotifyvis/utils.py | 43 ++++++++++++++++++++++++++++++++++++------- spotifyvis/views.py | 13 ++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/spotifyvis/utils.py b/spotifyvis/utils.py index 0fad798..235c958 100644 --- a/spotifyvis/utils.py +++ b/spotifyvis/utils.py @@ -28,7 +28,7 @@ def parse_library(headers, tracks, library_stats): # Track the number of samples for calculating # audio feature averages and standard deviations on the fly num_samples += 1 - get_track_info(track_dict['track'], library_stats) + get_track_info(track_dict['track'], library_stats, num_samples) # get_genre(headers, track_dict['track']['album']['id']) audio_features_dict = get_audio_features(headers, track_dict['track']['id']) for feature, feature_data in audio_features_dict.items(): @@ -115,8 +115,10 @@ def update_audio_feature_stats(feature, new_data_point, sample_size, library_sta cur_std_dev = library_stats['audio_features'][feature]['std_dev'] new_mean, new_std_dev = update_std_dev(cur_mean, cur_std_dev, new_data_point, sample_size) - library_stats['audio_features'][feature]['average'] = new_mean - library_stats['audio_features'][feature]['std_dev'] = new_std_dev + library_stats['audio_features'][feature] = { + "average": new_mean, + "std_dev": new_std_dev + } # increase_nested_key {{{ # @@ -161,20 +163,47 @@ def increase_artist_count(headers, artist_name, artist_id, library_stats): # }}} increase_artist_count # +def update_popularity_stats(new_data_point, library_stats, sample_size): + """Updates the popularity statistics in library_stats + + Args: + new_data_point: new data to update the popularity stats with + library_stats: Dictionary containing data mined from user's Spotify library + sample_size: The sample size including the new data + + Returns: + None + """ + if sample_size < 2: + library_stats['popularity'] = { + "average": new_data_point, + "std_dev": 0, + } + else : + cur_mean_popularity = library_stats['popularity']['average'] + cur_popularity_stdev = library_stats['popularity']['std_dev'] + new_mean, new_std_dev = update_std_dev( + cur_mean_popularity, cur_popularity_stdev, new_data_point, sample_size) + library_stats['popularity'] = { + "average": new_mean, + "std_dev": new_std_dev, + } + # get_track_info {{{ # -def get_track_info(track_dict, library_stats): +def get_track_info(track_dict, library_stats, sample_size): """Get all the info from the track_dict directly returned by the API call in parse_library. :track_dict: Dict returned from the API call containing the track info. :library_stats: Dictionary containing the data mined from user's Spotify library + :sample_size: The sample size so far including this track :returns: None """ - # popularity - library_stats['popularity'].append(track_dict['popularity']) - + # popularity + update_popularity_stats(track_dict['popularity'], library_stats, sample_size) + # year year_released = track_dict['album']['release_date'].split('-')[0] increase_nested_key('year_released', year_released, library_stats) diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 0e5816b..012a80e 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -139,7 +139,18 @@ def user_data(request): } tracks_to_query = 5 - library_stats = {"audio_features":{}, "genres":{}, "year_released":{}, "artists":{}, "num_songs":0, "popularity":[], "total_runtime":0} + library_stats = { + "audio_features":{}, + "genres":{}, + "year_released":{}, + "artists":{}, + "num_songs": 0, + "popularity": { + "average": 0, + "std_dev": 0, + }, + "total_runtime": 0 + } parse_library(headers, tracks_to_query, library_stats) return render(request, 'spotifyvis/user_data.html', context)