-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathONeills_blogpost3.py
executable file
·214 lines (182 loc) · 6.5 KB
/
ONeills_blogpost3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 9 12:43:17 2020
This code contributed to the blog post:
https://highnoongmt.wordpress.com/2020/03/15/an-analysis-of-the-365-double-jigs-in-oneills-pt-3/
@author: bobs
"""
import numpy as np
import matplotlib.pyplot as plt
import textdistance # Make sure you install jellyfish, it is fast! https://github.com/jamesturk/jellyfish
import re
import pandas as pd
def compare_strings(a, b):
# remove white spaces
a = ''.join(a.split())
b = ''.join(b.split())
score = textdistance.damerau_levenshtein.normalized_similarity(a, b)
return score
FILENAME = 'ONeillsJigs_parsed'
with open(FILENAME, encoding='utf-8') as f:
data = f.read()
files = data.split('\n\n')
dictionary = {
'title': [],
'time_signature': [],
'key': [],
'abcdata': []
}
for f in files:
regexp = r'^(T:)?(?P<title>.*)?(\nM:)?(?P<time_signature>.*)?(\nK:)?(?P<key>.*)?(\n)?(?P<abcdata>.*)?$'
m = re.match(regexp, f, re.M)
d = m.groupdict()
[dictionary[k].append(v) for k,v in d.items()]
df = pd.DataFrame.from_dict(dictionary)
numtunes = len(df)
#numtunes = 3
structure = []
for nn in range(numtunes):
trans = df.abcdata[nn]
trans_stripped = re.findall(r'(:\|)|(\|:)|(\|1)|(\|2)|( \| )',trans)
trans_stripped_flattened = "".join([item for sublist in trans_stripped for item in sublist])
trans_stripped_flattened = re.sub(r':\|','E',trans_stripped_flattened)
trans_stripped_flattened = re.sub(r'\|:','S',trans_stripped_flattened)
trans_stripped_flattened = re.sub(r'\|1','1',trans_stripped_flattened)
trans_stripped_flattened = re.sub(r'\|2','2',trans_stripped_flattened)
trans_stripped_flattened = "".join(trans_stripped_flattened.split())
structure.append(trans_stripped_flattened)
df['structure'] = structure
#%% find unique structures and their frequency of occurrance
barstructures = df.structure.unique()
len(barstructures)
vc = df['structure'].value_counts()
print(vc.to_string())
df.loc[df['structure']==vc.index[26]]
#%%
score = np.zeros((numtunes,numtunes))
for nn in range(numtunes):
tune1 = df.iloc[nn]
# compare to all tunes with same meter
#dfsubset = df[(df['time_signature'] == tune1.time_signature)]
#numtunes = len(dfsubset)
score[nn,nn]=1.0
for mm in range(nn+1,numtunes):
tune2 = df.iloc[mm]
score[nn,mm] = compare_strings(tune1.structure,tune2.structure)-0.0*np.random.uniform()
score[mm,nn] = score[nn,mm]
print(str(nn) + '/' + str(mm) + ': Score ' + str(score[nn,mm]))
score[numtunes-1,numtunes-1]=1.0
#%% make image of similarity matrix
scoreplot=score
for ii in range(len(score)):
scoreplot[ii][ii] = 1
params = {'legend.fontsize': 'x-large',
'figure.figsize': (10, 10),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.imshow((scoreplot),cmap="gray",origin='lower')
ax.yaxis.set(ticks=range(20,numtunes,20), ticklabels=range(20,numtunes,20))
ax.xaxis.set(ticks=range(20,numtunes,20), ticklabels=range(20,numtunes,20))
plt.xticks(rotation=90)
plt.clim(0,1)
plt.xlabel("O'Neill's Jig Number")
plt.ylabel("O'Neill's Jig Number")
plt.show()
#%% collapse similarity to find mean similarity of tunes
marginalscore=np.sum(score,axis=0)/numtunes
params = {'legend.fontsize': 'x-large',
'figure.figsize': (10, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.arange(numtunes)+1,marginalscore)
plt.xlabel("O'Neill's Jig Number")
plt.ylabel("Mean Norm. DL Similarity")
plt.xlim((1,numtunes))
ax.xaxis.set(ticks=range(20,numtunes,20), ticklabels=range(20,numtunes,20))
plt.xticks(rotation=90)
plt.ylim((0.14,1))
plt.grid()
plt.show()
#%%
params = {'legend.fontsize': 'x-large',
'figure.figsize': (10, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
fig = plt.figure()
ax = fig.add_subplot(111)
hist,bin_edges = np.histogram(np.hstack(score),bins=np.arange(0,1,0.02))
ax.bar(bin_edges[0:-1],np.log10(hist/2+0.1),width=0.02)
plt.xlabel('Normalized Damerau Levenshtein Similarity')
plt.ylabel('Log10 Number')
plt.xlim((0,1))
plt.ylim((0,4.5))
plt.grid()
plt.show()
# mode
bin_edges[np.where(hist==np.max(hist))]
#%% plot the mean DL similarity of tunes as a function of their lengths in characters
tunelengths = np.zeros(numtunes)
for nn in range(numtunes):
tunelengths[nn] = len(df.structure[nn])
params = {'legend.fontsize': 'x-large',
'figure.figsize': (10, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(np.log10(tunelengths[np.where(df.key=='Cmaj')]),
marginalscore[np.where(df.key=='Cmaj')],c='b',alpha=0)
for nn in range(numtunes):
plt.text(np.log10(tunelengths[nn]),marginalscore[nn],str(nn+1),
rotation=45,horizontalalignment="center",verticalalignment="center")
plt.xlabel("Log10 Tune Length")
plt.ylabel("Mean Norm. DL Similarity")
plt.grid()
plt.show()
#%%
from sklearn.manifold import MDS
embedding = MDS(n_components=2,dissimilarity='precomputed')
X_transformed = embedding.fit_transform(1.0-score)
#%%
params = {'legend.fontsize': 'x-large',
'figure.figsize': (10, 10),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(X_transformed[:,0],X_transformed[:,1],alpha=0)
#axislimits = [-1,1,-1,1]
for nn in range(numtunes):
#if ((X_transformed[nn,0]>axislimits[0]) & (X_transformed[nn,0]<axislimits[1]) &
# (X_transformed[nn,1]>axislimits[2]) & (X_transformed[nn,1]<axislimits[3])):
plt.text(X_transformed[nn,0],X_transformed[nn,1],str(nn+1),
rotation=90*np.random.uniform(),horizontalalignment="center",verticalalignment="center",
alpha=(1 - 0.8*np.sqrt((tunelengths[nn]-np.min(tunelengths))/(np.max(tunelengths)-np.min(tunelengths)))))
plt.grid()
#plt.axis(axislimits)
plt.show()
#%%
a = np.where(X_transformed[:,1] < 0)
len(a[0])
#X_transformed[a,:]
#df.structure[287-1]