Browse Source

Genre Artist Breakdown

Implemented the generation of genre artist breakdown data for the genre
graphs. More test cases needed.
master
Chris Shyi 7 years ago
parent
commit
54c541426c
  1. 3
      api/admin.py
  2. 5
      api/models.py
  3. 50
      api/tests.py
  4. 43
      api/utils.py
  5. 11
      api/views.py
  6. 14
      graphs/static/graphs/scripts/genre_graph.js
  7. 6
      graphs/templates/graphs/genre_graph.html

3
api/admin.py

@ -1,8 +1,9 @@
from django.contrib import admin from django.contrib import admin
from .models import Track, Artist, AudioFeatures, User
from .models import Track, Artist, AudioFeatures, User, Genre
# Register your models here. # Register your models here.
admin.site.register(Track) admin.site.register(Track)
admin.site.register(Artist) admin.site.register(Artist)
admin.site.register(AudioFeatures) admin.site.register(AudioFeatures)
admin.site.register(User) admin.site.register(User)
admin.site.register(Genre)

5
api/models.py

@ -30,6 +30,8 @@ class Artist(models.Model):
id = models.CharField(primary_key=True, max_length=MAX_ID) id = models.CharField(primary_key=True, max_length=MAX_ID)
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
genres = models.ManyToManyField(Genre, blank=True) genres = models.ManyToManyField(Genre, blank=True)
# genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True,
# null=True)
def __str__(self): def __str__(self):
return self.name return self.name
@ -45,7 +47,6 @@ class Track(models.Model):
verbose_name_plural = "Tracks" verbose_name_plural = "Tracks"
id = models.CharField(primary_key=True, max_length=MAX_ID) id = models.CharField(primary_key=True, max_length=MAX_ID)
# artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
artists = models.ManyToManyField(Artist, blank=True) artists = models.ManyToManyField(Artist, blank=True)
year = models.PositiveSmallIntegerField() year = models.PositiveSmallIntegerField()
popularity = models.PositiveSmallIntegerField() popularity = models.PositiveSmallIntegerField()
@ -53,7 +54,7 @@ class Track(models.Model):
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
users = models.ManyToManyField(User, blank=True) users = models.ManyToManyField(User, blank=True)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True, genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True,
null=True)
null=True)
def __str__(self): def __str__(self):
track_str = "{}, genre: {}, artists: [".format(self.name, self.genre) track_str = "{}, genre: {}, artists: [".format(self.name, self.genre)

50
api/tests.py

@ -0,0 +1,50 @@
from django.test import TestCase
from api.models import Track, Genre, Artist
from login.models import User
from api import utils
import math
import pprint
class GenreDataTestCase(TestCase):
def setUp(self):
test_user = User.objects.create(id="chrisshi", refresh_token="blah", access_token="blah",
access_expires_in=10)
genre = Genre.objects.create(name="classical", num_songs=3)
artist_1 = Artist.objects.create(id='art1', name="Beethoven")
artist_2 = Artist.objects.create(id='art2', name="Mozart")
artist_3 = Artist.objects.create(id='art3', name='Chopin')
track_1 = Track.objects.create(id='track1', year=2013,
popularity=5, runtime=20,
name='concerto1',
genre=genre)
track_1.users.add(test_user)
track_1.artists.add(artist_1)
track_1.artists.add(artist_2)
track_2 = Track.objects.create(id='track2', year=2013,
popularity=5, runtime=20,
name='concerto2',
genre=genre)
track_2.users.add(test_user)
track_2.artists.add(artist_2)
track_2.artists.add(artist_3)
track_2.artists.add(artist_1)
track_3 = Track.objects.create(id='track3', year=2013,
popularity=5, runtime=20,
name='concerto3',
genre=genre)
track_3.users.add(test_user)
track_3.artists.add(artist_1)
track_3.artists.add(artist_3)
def test_get_artist_counts_in_genre(self):
test_user = User.objects.get(id='chrisshi')
artist_counts = utils.get_artists_in_genre(test_user, 'classical', 10)
# pprint.pprint(artist_counts)
self.assertTrue(math.isclose(artist_counts['Beethoven'], 1.3, rel_tol=0.05))
self.assertTrue(math.isclose(artist_counts['Mozart'], 0.85, rel_tol=0.05))
self.assertTrue(math.isclose(artist_counts['Chopin'], 0.85, rel_tol=0.05))
self.assertTrue(math.isclose(sum(artist_counts.values()), 3, rel_tol=0.01))

43
api/utils.py

@ -11,6 +11,8 @@ from django.core import serializers
from django.utils import timezone from django.utils import timezone
from .models import * from .models import *
from login.models import User from login.models import User
from django.db.models import FloatField
from django.db.models.functions import Cast
# }}} imports # # }}} imports #
@ -34,16 +36,13 @@ def update_track_genres(user_obj):
user_tracks = Track.objects.filter(users__exact=user_obj) user_tracks = Track.objects.filter(users__exact=user_obj)
for track in user_tracks: for track in user_tracks:
# just using this variable to save another call to db # just using this variable to save another call to db
track_artists = track.artists.all()
# set genres to first artist's genres then find intersection with others
shared_genres = track_artists.first().genres.all()
for artist in track_artists:
shared_genres = shared_genres.intersection(artist.genres.all())
shared_genres = shared_genres.order_by('-num_songs')
track_artists = list(track.artists.all())
# TODO: Use the most popular genre of the first artist as the Track genre
first_artist_genres = track_artists[0].genres.all().order_by('-num_songs')
undefined_genre_obj = Genre.objects.get(name="undefined") undefined_genre_obj = Genre.objects.get(name="undefined")
most_common_genre = shared_genres.first() if shared_genres.first() is \
not undefined_genre_obj else shared_genres[1]
most_common_genre = first_artist_genres.first() if first_artist_genres.first() is \
not undefined_genre_obj else first_artist_genres[1]
track.genre = most_common_genre if most_common_genre is not None \ track.genre = most_common_genre if most_common_genre is not None \
else undefined_genre_obj else undefined_genre_obj
track.save() track.save()
@ -143,8 +142,7 @@ def process_artist_genre(genre_name, artist_obj):
:returns: None :returns: None
""" """
genre_obj, created = Genre.objects.get_or_create(name=genre_name,
defaults={'num_songs':1})
genre_obj, created = Genre.objects.get_or_create(name=genre_name, defaults={'num_songs': 1})
if not created: if not created:
genre_obj.num_songs = F('num_songs') + 1 genre_obj.num_songs = F('num_songs') + 1
genre_obj.save() genre_obj.save()
@ -192,7 +190,7 @@ def get_artists_in_genre(user, genre, max_songs):
"""Return count of artists in genre. """Return count of artists in genre.
:user: User object to return data for. :user: User object to return data for.
:genre: genre to count artists for.
:genre: genre to count artists for. (string)
:max_songs: max total songs to include to prevent overflow due to having :max_songs: max total songs to include to prevent overflow due to having
multiple artists on each track. multiple artists on each track.
@ -200,19 +198,22 @@ def get_artists_in_genre(user, genre, max_songs):
have. have.
""" """
genre_obj = Genre.objects.get(name=genre) genre_obj = Genre.objects.get(name=genre)
artist_counts = (Artist.objects.filter(track__users=user)
.filter(genres=genre_obj)
.annotate(num_songs=Count('track', distinct=True))
.order_by('-num_songs')
)
tracks_in_genre = Track.objects.filter(genre=genre_obj, users=user)
track_count = tracks_in_genre.count()
user_artists = Artist.objects.filter(track__users=user) # use this variable to save on db queries
total_artist_counts = tracks_in_genre.aggregate(counts=Count('artists'))['counts']
processed_artist_counts = {} processed_artist_counts = {}
songs_added = 0
for artist in artist_counts:
# songs_added = 0
for artist in user_artists:
# hacky way to not have total count overflow due to there being multiple # hacky way to not have total count overflow due to there being multiple
# artists on a track # artists on a track
if songs_added + artist.num_songs <= max_songs:
processed_artist_counts[artist.name] = artist.num_songs
songs_added += artist.num_songs
# if songs_added + artist.num_songs <= max_songs:
# processed_artist_counts[artist.name] = artist.num_songs
# songs_added += artist.num_songs
processed_artist_counts[artist.name] = round(artist.track_set
.filter(genre=genre_obj, users=user)
.count() * track_count / total_artist_counts, 2)
# processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts] # processed_artist_counts = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts]
# processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts} # processed_artist_counts = {artist.name: artist.num_songs for artist in artist_counts}
# pprint.pprint(processed_artist_counts) # pprint.pprint(processed_artist_counts)

11
api/views.py

@ -174,13 +174,12 @@ def get_genre_data(request, user_secret):
""" """
user = User.objects.get(secret=user_secret) user = User.objects.get(secret=user_secret)
genre_counts = (Track.objects.filter(users__exact=user) genre_counts = (Track.objects.filter(users__exact=user)
.values('genre')
.order_by('genre')
.annotate(num_songs=Count('genre'))
)
.values('genre')
.order_by('genre')
.annotate(num_songs=Count('genre'))
)
for genre_dict in genre_counts: for genre_dict in genre_counts:
genre_dict['artists'] = get_artists_in_genre(user, genre_dict['genre'],
genre_dict['num_songs'])
genre_dict['artists'] = get_artists_in_genre(user, genre_dict['genre'], genre_dict['num_songs'])
print("*** Genre Breakdown ***") print("*** Genre Breakdown ***")
pprint.pprint(list(genre_counts)) pprint.pprint(list(genre_counts))
return JsonResponse(data=list(genre_counts), safe=False) return JsonResponse(data=list(genre_counts), safe=False)

14
graphs/static/graphs/scripts/genre_graph.js

@ -4,7 +4,7 @@ function create_genre_graph(data) {
data.forEach(function(d) { data.forEach(function(d) {
d.num_songs = +d.num_songs; d.num_songs = +d.num_songs;
console.log(d.genre, d.num_songs); console.log(d.genre, d.num_songs);
var artist_names = Object.keys(d.artists);
let artist_names = Object.keys(d.artists);
artist_names.forEach(function(e) { artist_names.forEach(function(e) {
d.artists[e] = +d.artists[e]; d.artists[e] = +d.artists[e];
console.log(e, d.artists[e]); console.log(e, d.artists[e]);
@ -31,22 +31,22 @@ function create_genre_graph(data) {
// setup bar colors {{{ // // setup bar colors {{{ //
var max_artists = d3.max(data, function(d) {
let max_artists = d3.max(data, function(d) {
return Object.keys(d.artists).length; return Object.keys(d.artists).length;
}); });
var z = d3.scaleOrdinal().range(randomColor({
let z = d3.scaleOrdinal().range(randomColor({
count: max_artists, count: max_artists,
luminosity: 'light', luminosity: 'light',
})); }));
// }}} setup bar colors // // }}} setup bar colors //
for (var genre_dict of data) {
for (let genre_dict of data) {
// process artist breakdown {{{ // // process artist breakdown {{{ //
var keys = Object.keys(genre_dict.artists);
var stack = d3.stack()
let keys = Object.keys(genre_dict.artists);
let stack = d3.stack()
//.order(d3.stackOrderAscending) //.order(d3.stackOrderAscending)
.order(d3.stackOrderDescending) .order(d3.stackOrderDescending)
.keys(keys)([genre_dict.artists]) .keys(keys)([genre_dict.artists])
@ -112,7 +112,7 @@ function create_genre_graph(data) {
// https://gist.github.com/guypursey/f47d8cd11a8ff24854305505dbbd8c07#file-index-html // https://gist.github.com/guypursey/f47d8cd11a8ff24854305505dbbd8c07#file-index-html
function wrap(text, width) { function wrap(text, width) {
text.each(function() { text.each(function() {
var text = d3.select(this),
let text = d3.select(this),
words = text.text().split(/\s+/).reverse(), words = text.text().split(/\s+/).reverse(),
word, word,
line = [], line = [],

6
graphs/templates/graphs/genre_graph.html

@ -26,16 +26,16 @@
<svg width="1920" height="740"></svg> <svg width="1920" height="740"></svg>
<script> <script>
var svg = d3.select("svg"),
let svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40}, margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right, width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom, height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleBand()
let x = d3.scaleBand()
.rangeRound([0, width]) .rangeRound([0, width])
.paddingInner(0.05) .paddingInner(0.05)
.align(0.1); .align(0.1);
var y = d3.scaleLinear()
let y = d3.scaleLinear()
.rangeRound([height, 0]); .rangeRound([height, 0]);
d3.json("{% url "api:get_genre_data" user_secret %}").then(create_genre_graph); d3.json("{% url "api:get_genre_data" user_secret %}").then(create_genre_graph);

Loading…
Cancel
Save