-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensemble.py
378 lines (254 loc) · 11.9 KB
/
ensemble.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import numpy as np
import matplotlib.pyplot as plt
from collections import defaultdict
from Bio import AlignIO
from Bio import SeqIO, Phylo
from Bio.Align import MultipleSeqAlignment
from Bio.Phylo.TreeConstruction import DistanceCalculator, DistanceTreeConstructor
from music21 import instrument, note, stream, scale
from music21.instrument import StringInstrument, WoodwindInstrument, BrassInstrument, Percussion, PitchedPercussion
from music21 import midi
import itertools
from algorithms import *
import sys
import os
import shutil
from config import GLOBALS, OUTPUT_FILES, SEQ_DIR
class Composer(object):
alignment = None
def __init__(self, cluster_algorithm, pitch_algorithm,
duration_algorithm, dynamics_algorithm,
input_type='alignment', **kwargs):
assert isinstance(cluster_algorithm, ClusteringAlgorithm) \
and isinstance(pitch_algorithm, PitchAlgorithm) \
and isinstance(duration_algorithm, DurationsAlgorithm)\
and isinstance(dynamics_algorithm, DynamicsAlgorithm)\
and input_type
if input_type in ['alignment', 'array']:
assert 'alignment' in kwargs.keys()
self.alignment = kwargs['alignment']
elif input_type == 'sequences':
assert 'fasta' in kwargs.keys()
seq_file = kwargs['fasta_file']
if 'algorithm' not in kwargs.keys():
print('Missing alignment algorithm')
aln_algorithm = kwargs['algorithm']
seq_vector, n_seq = None, None
if 'seq_vector' not in kwargs.keys():
if 'n_seq' not in kwargs.keys():
print('Missing explicit vector of sequences or maximum number of sequences')
sys.exit(1)
n_seq = kwargs['n_seq']
else:
seq_vector = kwargs['seq_vector']
from core import gen_alignment
self.alignment = gen_alignment(seq_file, seq_vector=seq_vector, n_sequences=n_seq, algorithm=aln_algorithm)
self.clustering_algorithm = cluster_algorithm
self.durations_algorithm = duration_algorithm
self.pitch_algorithm = pitch_algorithm
self.dynamics_algorithm = dynamics_algorithm
def assign_instruments(self):
# assert isinstance(self.alignment, MultipleSeqAlignment)
return [instrument.Timpani(), instrument.Glockenspiel(), instrument.Vibraphone(), instrument.Marimba()]
def gen_numerical_vectors(self, k=2, piece_length=5):
msa = AlignIO.read(self.alignment, 'clustal') if not isinstance(self.alignment, np.ndarray) else self.alignment
window_sizes = np.zeros(3)
if 'windows_size' in self.dynamics_algorithm.keys():
window_sizes[0] = self.clustering_algorithm['windows_size']
if 'windows_size' in self.pitch_algorithm.keys():
window_sizes[1] = self.pitch_algorithm['windows_size']
if 'windows_size' in self.durations_algorithm.keys():
window_sizes[2] = self.durations_algorithm['windows_size']
iterator = iter(window_sizes)
window_size = next(iterator)
window_sizes_are_valid = np.all(window_size == rest for rest in iterator if rest != 0)
assert window_sizes_are_valid, 'Window sizes cannot differ in algorithm mappings'
from core import gen_song
# TODO: insert clustering/instrument assigning algorithm
instruments = self.assign_instruments()[0:len(msa)]
songs = gen_song(self.pitch_algorithm, self.durations_algorithm, self.dynamics_algorithm, msa, instruments, k, piece_length=piece_length)
assert isinstance(songs, list)
return songs
# dynamics_vector = gen_dynamics_vector(msa, self.dynamics_algorithm)
# add_dynamics_to_score(dynamics_vector['vol'], score)
def play(self):
pass
def shuffle_instruments(self):
pass
class FileWriter(object):
def __init__(self, score, records):
self.score = score
self.records = records
assert isinstance(score, stream.Score) and isinstance(records, np.ndarray)
print(score.parts, records)
assert len(score.parts) == len(records), str(len(score.parts)) + ' ' + str(len(records))
def write(self, name='alignment', display=False, stats=None, **kwargs):
if len(kwargs.keys()) == 0:
print('No output type or path specified')
sys.exit(1)
if not os.path.isdir(name):
#shutil.rmtree(name)
os.mkdir(name)
subdir = None
if 'subdir' in kwargs.keys():
subdir = kwargs['subdir']
# parsing arguments
for key, value in kwargs.items():
print(type(value))
if key == 'midi':
print('Writing midi...')
if not value.endswith('.mid'):
value += '.mid'
if not os.path.isdir(GLOBALS['MIDI']):
os.mkdir(GLOBALS['MIDI'])
output_midi = GLOBALS['MIDI'] + '/'
if subdir:
# shutil.rmtree(output_midi + subdir)
output_midi += subdir + '/'
if not os.path.isdir(output_midi):
os.mkdir(output_midi)
output_midi += value
f = midi.translate.streamToMidiFile(self.score)
print('Output MIDI', output_midi)
f.open(output_midi, attrib='wb')
f.write()
f.close()
import subprocess
# currently audio can only be generated from MIDI input
if 'audio' in kwargs.keys():
audio_name = output_midi.split('.mid')[0] + '.ogg'
print('AUDIO NAME', audio_name)
subprocess.call(["timidity", output_midi, "-Ow", "-o", audio_name])
elif key == 'score':
print('Writing score...')
# lily already inserts extension in filename
value = value[:-4] if value.endswith('.pdf') else value
path = GLOBALS['SCORES'] + '/'
if subdir:
# shutil.rmtree(path + subdir)
path += subdir + '/'
if os.path.isdir(path):
os.mkdir(path)
path += value
self.score.write('lily.pdf', fp=path)
# deleting tmp file created by lily without .pdf extension
to_unlink = GLOBALS['SCORES'] + '/' + value
if os.path.isfile(to_unlink):
os.unlink(to_unlink)
if display:
print('Displaying score...')
self.score.show(app=GLOBALS['MUSE_SCORE'])
if stats:
assert isinstance(stats, str), 'Invalid path for stats file'
print('Generating pitch and duration histograms...')
hist_durations_dir = GLOBALS['HIST_DURATIONS']
if not os.path.isdir(hist_durations_dir):
os.makedirs(hist_durations_dir)
hist_notes_dir = GLOBALS['HIST_NOTES']
if not os.path.isdir(hist_notes_dir):
os.makedirs(hist_notes_dir)
i = 0
for part in self.score.parts:
durations_notes = np.array([(float(x.duration.quarterLength), x.name) for x in part
if isinstance(x, note.Note)],
dtype=[('durations', np.float,), ('notes', 'S5')])
durations = durations_notes['durations'] # first column
print('Durations', durations)
print('Durations set', np.unique(durations))
notes = durations_notes['notes']
plt.hist(durations, color='b')
plt.savefig(hist_durations_dir + '/' + stats + '_' + self.records[i] + '.png')
plt.close()
from collections import Counter
counts = Counter(notes)
note_names = counts.keys()
note_values = counts.values()
width = 1.0
idx = np.arange(len(note_names))
plt.bar(idx, note_values, width)
plt.xticks(idx + width * 0.5, note_names)
plt.savefig(hist_notes_dir + '/' + stats + '_' + self.records[i] + '.png')
plt.close()
i += 1
# TODO:
# check if test file with results already exist 2 mins
# if not:
# test BioPython
# test music21
# check if lily is in music21 environment
# check if muse score is in music21 environment
# check if pyqt is installed
# check if scipy is installed
# else:
# check file content for success or failure
def config_environment():
pass
def run():
#### config_environment() ####
# test vectors
# hand-made file
# piece-length = 15
# window = 5
msa = np.array(
[
['AAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCACGCTGTCAA'],
['AAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCAGCATCTCAG'],
['AAACGAAACGAAACGAAACGCCCCCCCCCCCCCCCCAGCAGTCAA'],
['AAATCAAATCAAATCAAATCCCCCCCCCCCCCCCCTTAGCGCTTA']
]
)
aln = np.empty((len(msa), len(list(msa[0])[0])), dtype="S1")
for i in range(0, len(msa)):
seq = list(msa[i])[0]
for j in range(0, len(seq)):
aln[i][j] = seq[j]
msa = aln
pitch_algo = PitchAlgorithm(PitchAlgorithm.WORD_DISTANCES, scale=scale.MajorScale().getPitches(), n_nucleotides=1)
durations_algo = DurationsAlgorithm(DurationsAlgorithm.FREQUENCIES_DYNAMIC, window_size=2, window_duration=10, n_nucleotides=1)
dynamics_algo = DynamicsAlgorithm(DynamicsAlgorithm.SHANNON_INDEX, window_size=2, gap_window_threshold=0.5, gap_column_threshold=0.7, criteria='local', levels=7)
instruments_algo = ClusteringAlgorithm('kmeans')
composer = Composer(instruments_algo, pitch_algo, durations_algo, dynamics_algo, input_type='array',
alignment=msa)
sequence_names = np.array([str(i) for i in range(0, len(msa))])
i = 0
scores = composer.gen_numerical_vectors(k=2)
for score in scores:
fw = FileWriter(score, sequence_names)
fname = 'hardcoded_test_' + str(i)
print('fname', fname)
fw.write(midi=fname, audio=fname, display=False)
i += 1
"""aln_file = SEQ_DIR + '/clustal3.aln'
from music21 import scale
pitch_algo = PitchAlgorithm(PitchAlgorithm.WORD_DISTANCES, scale=scale.MajorScale().getPitches(), n_nucleotides=2)
durations_algo = DurationsAlgorithm(DurationsAlgorithm.FREQUENCIES_DYNAMIC, window_size=1000, window_duration=500, n_nucleotides=2)
dynamics_algo = DynamicsAlgorithm(DynamicsAlgorithm.SHANNON_INDEX, window_size=2500, gap_window_threshold=0.5, gap_column_threshold=0.7, criteria='local', levels=7)
instruments_algo = ClusteringAlgorithm('kmeans')
composer = Composer(instruments_algo, pitch_algo, durations_algo, dynamics_algo, input_type='alignment', alignment=aln_file)
msa = AlignIO.read(aln_file, 'clustal')
sequences = np.chararray(len(msa), itemsize=25)
i = 0
for record in msa:
sequences[i] = record.description
i += 1
i = 0
for k in range(2, 7):
scores = composer.gen_numerical_vectors(k=k)
for score in scores:
fw = FileWriter(score, sequences)
fname = 'test_fast_' + str(i) + "_" + str(k)
print 'fname', fname
fw.write(midi=fname, audio=fname, display=False)
i += 1"""
print('Done')
if __name__ == "__main__":
import multiprocessing
import time
p = multiprocessing.Process(target=run, name="Run", args=())
p.start()
print('Started!')
time.sleep(20 * 60)
if p.is_alive():
print('Early kill')
p.terminate()
p.join()