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.
This commit is contained in:
@@ -28,7 +28,7 @@ def parse_library(headers, tracks, library_stats):
|
|||||||
# Track the number of samples for calculating
|
# Track the number of samples for calculating
|
||||||
# audio feature averages and standard deviations on the fly
|
# audio feature averages and standard deviations on the fly
|
||||||
num_samples += 1
|
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'])
|
# get_genre(headers, track_dict['track']['album']['id'])
|
||||||
audio_features_dict = get_audio_features(headers, track_dict['track']['id'])
|
audio_features_dict = get_audio_features(headers, track_dict['track']['id'])
|
||||||
for feature, feature_data in audio_features_dict.items():
|
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']
|
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)
|
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] = {
|
||||||
library_stats['audio_features'][feature]['std_dev'] = new_std_dev
|
"average": new_mean,
|
||||||
|
"std_dev": new_std_dev
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# increase_nested_key {{{ #
|
# increase_nested_key {{{ #
|
||||||
@@ -161,20 +163,47 @@ def increase_artist_count(headers, artist_name, artist_id, library_stats):
|
|||||||
|
|
||||||
# }}} increase_artist_count #
|
# }}} 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 {{{ #
|
# 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.
|
"""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.
|
:track_dict: Dict returned from the API call containing the track info.
|
||||||
:library_stats: Dictionary containing the data mined from user's Spotify library
|
:library_stats: Dictionary containing the data mined from user's Spotify library
|
||||||
|
:sample_size: The sample size so far including this track
|
||||||
|
|
||||||
:returns: None
|
:returns: None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# popularity
|
# popularity
|
||||||
library_stats['popularity'].append(track_dict['popularity'])
|
update_popularity_stats(track_dict['popularity'], library_stats, sample_size)
|
||||||
|
|
||||||
# year
|
# year
|
||||||
year_released = track_dict['album']['release_date'].split('-')[0]
|
year_released = track_dict['album']['release_date'].split('-')[0]
|
||||||
increase_nested_key('year_released', year_released, library_stats)
|
increase_nested_key('year_released', year_released, library_stats)
|
||||||
|
|||||||
@@ -139,7 +139,18 @@ def user_data(request):
|
|||||||
}
|
}
|
||||||
|
|
||||||
tracks_to_query = 5
|
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)
|
parse_library(headers, tracks_to_query, library_stats)
|
||||||
return render(request, 'spotifyvis/user_data.html', context)
|
return render(request, 'spotifyvis/user_data.html', context)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user