Created
October 8, 2021 15:13
-
-
Save irishryoon/f258dd53c44babdb33c4e1a0c0fcfaa9 to your computer and use it in GitHub Desktop.
Code for building artist graph in BFS way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def continue_building_graph(G, queue, visited, ID_artists, max_count, sp): | |
"""Builds a weighted graph G using BFS. | |
Given a (possibly empty) graph G and a (possibly empty) queue, | |
(1) create nodes for new artists, | |
(2) create edges for new collaborations, | |
(3) update the 'albums' attribute of an edge to keep track of all albums accounted for. | |
In the very last step, find the weight of each edge as the number of albums | |
in the 'albums' attribute. | |
Furthermore, update the dictionary of artist ID and names. | |
Parameters | |
---------- | |
G: (networkx graph) Can be empty or non-empty | |
queue: (collections.deque) of artists (IDs) to be explored | |
visited: (set) of artists (IDs) who have been visited | |
ID_artists: (dict) of artists' Spotify ID and names | |
key: ID | |
value: artist name | |
max_count: (int) number of artists to visit | |
sp: spotipy.client.Spotify object | |
Returns | |
------- | |
G: (networkx graph) updated graph | |
queue: (collections.deque) updated queue | |
visited: (set) updated set of visited artists | |
ID_artists: (dict) updated dictionary of artist ID and names | |
""" | |
count = 0 | |
while queue and (count < max_count): | |
current_uri = queue.popleft() | |
if current_uri in visited: | |
pass | |
else: | |
visited.add(current_uri) | |
# get albums, collaborative artists | |
collab_artists, IDs = get_collaborators(current_uri, sp) | |
# update 'ID_artists' | |
ID_artists = {**ID_artists, **IDs} | |
# go through each album | |
for (album, album_artists) in collab_artists.items(): | |
# go through each collection of collaborators in the album | |
for collection in album_artists: | |
# check if the collection consists of more than one artist. | |
if len(collection) > 1: | |
# create artist nodes as needed | |
for artist in collection: | |
if artist not in G: | |
G.add_node(artist) | |
# examine each pair of artists | |
# create new edge or update the 'albums' attribute of existing edge | |
for pair in itertools.combinations(collection,2): | |
# update edge | |
if G.has_edge(pair[0], pair[1]): | |
G[pair[0]][pair[1]]['albums'].add(album) | |
# create edge | |
else: | |
G.add_edge(pair[0], pair[1], weight = 1, albums = {album}) | |
# add collaborative artists to the Queue | |
for artist in collection: | |
if artist not in visited: | |
queue.append(artist) | |
count += 1 | |
# compute edge weights | |
for n1,n2,edge in G.edges(data=True): | |
edge['weight'] = len(edge['albums']) | |
return G, queue, visited, ID_artists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment