In [ ]:
from compmusic import dunya
from compmusic.dunya import makam
import codecs, pickle, csv
import networkx as nx

dunya.set_token('3d62cb5ea235fb739d85752677695594fe40888c')

Get artists


In [ ]:
artist_file = codecs.open('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/artists.csv', 'w', encoding='utf-8')

In [ ]:
artists = makam.get_artists()
artists = [[a['mbid'], a['name']] for a in artists]
for a in artists:
    artist_file.write(','.join(a))
    artist_file.write('\n')
artist_file.close()

Graph from the relations


In [ ]:
pc_g = nx.DiGraph()
pp_g = nx.Graph()

recordings


In [ ]:
recs = makam.get_recordings()

In [ ]:
for rec in recs:
    print rec['mbid']
    recinfo = makam.get_recording(rec['mbid'])
    
    #co-performers
    performers = []
    if 'performers' in recinfo.keys():
        performers = [p['mbid'] for p in recinfo['performers']]
    if 'releases' in recinfo.keys():
        rels = [r['mbid'] for r in recinfo['releases']]
        rel_artists = []
        for rel in rels:
            relinfo = makam.get_release(rel)
            if 'artists' in relinfo.keys():
                rel_artists.extend([a['mbid'] for a in relinfo['artists']])
            if 'release_artists' in relinfo.keys():
                rel_artists.extend([a['mbid'] for a in relinfo['release_artists']])
        performers.extend(rel_artists)
    performers = unique(performers)
    
    if len(performers) > 1:
        for i in xrange(len(performers)):
            for j in xrange(i+1, len(performers)):
                if performers[i] in pp_g.nodes():
                    if performers[j] in pp_g[performers[i]].keys():
                        pp_g[performers[i]][performers[j]]['weight'] += 1
                    else:
                        pp_g.add_edge(performers[i], performers[j], attr_dict={'weight': 1})
                else:
                    pp_g.add_edge(performers[i], performers[j], attr_dict={'weight': 1})
    
    #performer-composer, performer-lyricist
    composers = []
    lyricists = []
    if 'works' in recinfo.keys():
        works = [w['mbid'] for w in recinfo['works']]
        for work in works:
            workinfo = makam.get_work(work)
            if 'composers' in workinfo.keys():
                composers.extend([c['mbid'] for c in workinfo['composers']])
            if 'lyricists' in workinfo.keys():
                lyricists.extend([l['mbid'] for l in workinfo['lyricists']])
    composers = unique(composers)
    lyricists = unique(lyricists)

    for p in performers:
        for c in composers:
            if p in pc_g.nodes():
                if c in pc_g[p].keys():
                    pc_g[p][c]['weight'] += 1
                else:
                    pc_g.add_edge(p, c, attr_dict={'weight': 1})
            else:
                pc_g.add_edge(p, c, attr_dict={'weight': 1})
                
        for c in lyricists:
            if p in pc_g.nodes():
                if c in pc_g[p].keys():
                    pc_g[p][c]['weight'] += 1
                else:
                    pc_g.add_edge(p, c, attr_dict={'weight': 1})
            else:
                pc_g.add_edge(p, c, attr_dict={'weight': 1})

In [ ]:
print pp_g.number_of_nodes(), pp_g.number_of_edges()
print pc_g.number_of_nodes(), pc_g.number_of_edges()

In [ ]:
pickle.dump(pc_g, file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pc.pickle', 'w'))
nx.write_graphml(pc_g, '/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pc.gml', encoding='utf-8')

pickle.dump(pp_g, file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pp.pickle', 'w'))
nx.write_graphml(pp_g, '/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pp.gml', encoding='utf-8')

Replace MBIDs with names in artist graph


In [ ]:
pc_g = pickle.load(file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pc.pickle'))
pp_g = pickle.load(file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pp.pickle'))

artist_file = codecs.open('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/artists.csv', 
                          'r', encoding='utf-8').readlines()
artist_file = [a.encode('utf-8') for a in artist_file]
reader = csv.reader(artist_file)
artists = {row[0]:row[1].decode('utf-8') for row in reader}

In [ ]:
pc_g_named = nx.DiGraph()
pp_g_named = nx.Graph()

In [ ]:
for edge in pc_g.edges(data=True):
    pc_g_named.add_edge(artists[edge[0]], artists[edge[1]], attr_dict=edge[2])
    
for edge in pp_g.edges(data=True):
    pp_g_named.add_edge(artists[edge[0]], artists[edge[1]], attr_dict=edge[2])

In [ ]:
print pp_g.number_of_nodes(), pp_g.number_of_edges()
print pp_g_named.number_of_nodes(), pp_g_named.number_of_edges()
print pc_g.number_of_nodes(), pc_g.number_of_edges()
print pc_g_named.number_of_nodes(), pc_g_named.number_of_edges()

In [ ]:
pickle.dump(pc_g_named, file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pc-named.pickle', 'w'))
nx.write_graphml(pc_g_named, '/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pc-named.graphml', encoding='utf-8')

pickle.dump(pp_g_named, file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pp-named.pickle', 'w'))
nx.write_graphml(pp_g_named, '/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pp-named.graphml', encoding='utf-8')

Update artist file


In [ ]:
import csv, codecs
import pickle

In [ ]:
g = pickle.load(file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph.pickle'))
artist_file = codecs.open('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/artists.csv', 'r', encoding='utf-8').readlines()
artist_file = [a.encode('utf-8') for a in artist_file]
reader = csv.reader(artist_file)
cur_artists = [row[0] for row in reader]

In [ ]:
new_artists = set(g.nodes()) - set(cur_artists)
for mbid in new_artists:
    try:
        artist_info = makam.get_artist(mbid)
        artist_file.append(','.join([artist_info['mbid'], artist_info['name']])+',\n')
    except:
        try:
            artist_info = makam.get_composer(mbid)
            artist_file.append(','.join([artist_info['mbid'], artist_info['name']])+',\n')
        except:
            print mbid

In [ ]:
for i in xrange(len(cur_artists), len(artist_file)):
    artist_file[i] = artist_file[i].encode('utf-8')

In [ ]:
len(artist_file)

In [ ]:
out_file = codecs.open('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/artists.csv', 'w')

reader = csv.reader(artist_file)
rows = [row for row in reader]

writer = csv.writer(out_file)
writer.writerows(rows)
out_file.close()

Connectivity analysis


In [ ]:
pc_g = pickle.load(file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pc-named.pickle'))
pp_g = pickle.load(file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/graph-pp-named.pickle'))

artist_file = codecs.open('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/artists.csv', 
                          'r', encoding='utf-8').readlines()
artist_file = [a.encode('utf-8') for a in artist_file]
reader = csv.reader(artist_file)
nationality = {row[1].decode('utf-8'):row[2].decode('utf-8') for row in reader}

Pie chart of countries


In [ ]:
countries = {}
for a, c in nationality.items():
    if c == '':
        c = ['Turkey']
    else:
        c = c.split(', ')
    for i in c:
        if i in countries.keys():
            countries[i].append(a)
        else:
            countries[i] = [a]

In [ ]:
for c, people in countries.items():
    print c
    print ', '.join(people)
    print

In [ ]:
pie_data = array([[c, len(people)] for c, people in countries.items()])
total = sum(pie_data[:,1].astype(int))
pie_data[:, 1] = pie_data[:, 1].astype(int)/(1.0*total)

In [ ]:
%matplotlib tk
pie(pie_data[:,1], labels=pie_data[:,0])

Popularity of composers by nationality


In [ ]:
performance_counts = {}
for a, count in pc_g.in_degree().items():
    for c, people in countries.items():
        if a in people:
            if c in performance_counts.keys():
                performance_counts[c] += count
            else:
                performance_counts[c] = count

In [ ]:
for c, people in countries.items():
    if c in performance_counts.keys():
        performance_counts[c] = round(1.0*performance_counts[c]/len(people), 2)

In [ ]:
performance_counts = sorted(performance_counts.items(), key=lambda x:x[1], reverse=True)
for c, avg_count in performance_counts:
    print '\t\t'.join([c[:4], str(avg_count), str(len(countries[c]))])

Interracial composer-performer relations


In [ ]:
cp_countries = nx.DiGraph()

In [ ]:
for edge in pc_g_named.edges(data=True):
    i = edge[0]
    j = edge[1]
    countries_i = []
    countries_j = []
    for c, people in countries.items():
        if i in people:
            countries_i.append(c)
        if j in people:
            countries_j.append(c)
    
    for ci in countries_i:
        for cj in countries_j:
            if ci in cp_countries.nodes():
                if cj in cp_countries[ci].keys():
                    cp_countries[ci][cj]['weight'] += edge[2]['weight']
                else:
                    cp_countries.add_edge(ci, cj, attr_dict=edge[2])
            else:
                cp_countries.add_edge(ci, cj, attr_dict=edge[2])
cp_countries.remove_node('?')

In [ ]:
cp_countries.edges(data=True)

In [ ]:
pickle.dump(cp_countries, file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/pc_countries.pickle', 'w'))
nx.write_graphml(cp_countries, '/homedtic/gkoduri/workspace/collaborations/makamgraph/data/pc_countries.graphml', encoding='utf-8')

Interracial performer-performer relations


In [ ]:
pp_countries = nx.Graph()

In [ ]:
for edge in pp_g_named.edges(data=True):
    i = edge[0]
    j = edge[1]
    countries_i = []
    countries_j = []
    for c, people in countries.items():
        if i in people:
            countries_i.append(c)
        if j in people:
            countries_j.append(c)
    
    for ci in countries_i:
        for cj in countries_j:
            if ci in pp_countries.nodes():
                if cj in pp_countries[ci].keys():
                    pp_countries[ci][cj]['weight'] += edge[2]['weight']
                else:
                    pp_countries.add_edge(ci, cj, attr_dict=edge[2])
            else:
                pp_countries.add_edge(ci, cj, attr_dict=edge[2])
pp_countries.remove_node('?')

In [ ]:
cp_countries.edges(data=True)

In [ ]:
pickle.dump(pp_countries, file('/homedtic/gkoduri/workspace/collaborations/makamgraph/data/pp_countries.pickle', 'w'))
nx.write_graphml(pp_countries, '/homedtic/gkoduri/workspace/collaborations/makamgraph/data/pp_countries.graphml', encoding='utf-8')

In [ ]: