/
hasher.py
901 lines (766 loc) · 32.4 KB
/
hasher.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# Name: alpha/analysis/hasher.py
# Purpose: Hash musical notation
#
# Authors: Emily Zhang
#
# Copyright: Copyright © 2015 Michael Scott Cuthbert and the music21 Project
# License: BSD, see license.txt
# ------------------------------------------------------------------------------
import unittest
import collections
import difflib
from music21 import note, chord, key
from music21 import interval
from music21 import stream
from music21.exceptions21 import StreamException
class Hasher:
'''
This is a modular hashing object that can hash notes, chords, and rests, and some of their
properties. Steps to using and calling the hasher:
1) Initialize a hasher object
2) Set the properties that you want to hash. There are 4 main groups of properties/settings::
a) self.validTypes should be some combination of notes, chords, rests
b) general hashing settings include self.stripTies and self.includeReference. if
self.includeReference is True, a reference to to original note/rest/chord is created
and kept track of during the hashing process.
c) note properties are things like pitch, duration, offset, and some slightly fancier
properties
d) self.stateVars is a dictionary of things you might want to hash that require some memory
e.g. current key signature, interval from the last note
3) call the hashStream() function on the stream you want to hash.
This is what the Hasher object does the in background once hashStream() is called:
1) It runs self.setupValidTypesAndStateVars() and sets up properties from (a) and (d) from
above based on your settings
2) It runs self.preprocessStream() and based on settings from (d)
3) It determines which objects in the passed-in stream should be hashed
4) It runs self.setupTupleList() and sets up self.tupleList, self.hashingFunctions
and self.tupleClass, all related to each other. self.tupleList is a list of all the
properties that are hashed. self.hashingFunctions is a dictionary of which hashing function
should be used for each property (there are multiple ways of hashing a note's pitch, for
example, by MIDI number, or by a string representation). self.tupleClass is a NamedTuple
that is constructed ad hoc based on which properties are to be hashed.
5) For all the elements from the stream that are to be hashed, the hasher hashes every one of
its properties that are to be hashed using the hashing function listed in
self.hashingFunctions. It creates a single NamedTuple called a NoteHash for each element
from the stream. However, if self.includeReference is set to True, a NoteHashWithReference
tuple is created instead.
'''
def __init__(self):
'''
The Hasher object is initialized with defaults of what objects should be hashed, and
what properties of those objects should be hashed.
'''
# --- begin general types of things to hash ---
self.validTypes = [note.Note, note.Rest, chord.Chord]
# --- end general types of things to hash ---
# --- begin general hashing settings ---
self.includeReference = False
self.stripTies = True
# --- end general hashing settings ---
# --- begin note properties to hash ---
self.hashPitch = True
# hashMIDI = True => 58 instead of 'C--'
self.hashMIDI = True
# hashNoteNameOctave = False => 'C4' instead of 'C'
self.hashNoteNameOctave = False
self.hashOctave = False
self.hashDuration = True
self.roundDurationAndOffset = True
self.hashOffset = True
# self.roundOffset = True
self.granularity = 32
self.hashIntervalFromLastNote = False
self.hashIsAccidental = False
self.hashIsTied = False
# --- end note properties to hash ---
# --- begin chord properties to hash --- #
# chords can hashed as chords or by their note constituents
self.hashChordsAsNotes = True
self.hashChordsAsChords = False
self.hashNormalOrderString = False
self.hashPrimeFormString = False
# --- end chord properties to hash --- #
self.tupleList = []
self.tupleClass = None
# stateVars are variables that are kept track of through multiple hashes
# e.g. interval from the previous note, key signature
self.stateVars = {}
self.hashingFunctions = {}
def setupValidTypesAndStateVars(self):
'''
Sets up the self.stateVars dictionary depending on how the flags for
self.hashIntervalFromLastNote and self.hashIsAccidental are set.
>>> h = alpha.analysis.hasher.Hasher()
>>> h.hashIntervalFromLastNote = True
>>> h.setupValidTypesAndStateVars()
>>> h.stateVars
{'IntervalFromLastNote': None}
>>> h2 = alpha.analysis.hasher.Hasher()
>>> h2.hashIsAccidental = True
>>> h2.setupValidTypesAndStateVars()
>>> h2.stateVars
{'KeySignature': None}
>>> key.KeySignature in h2.validTypes
True
'''
if self.hashIntervalFromLastNote:
self.stateVars['IntervalFromLastNote'] = None
if self.hashIsAccidental:
self.validTypes.append(key.KeySignature)
self.stateVars['KeySignature'] = None
# -- Begin Individual Hashing Functions of Properties---
def _hashDuration(self, e, thisChord=None):
'''
returns the duration of a chord object passed in, otherwise the duration of a note
object passed in.
>>> h = alpha.analysis.hasher.Hasher()
>>> n = note.Note('A-', quarterLength=2.5)
>>> h._hashDuration(n)
2.5
>>> d = duration.Duration(2.0)
>>> c = chord.Chord('A-4 C#5 E5', duration=d)
>>> h._hashDuration(n, thisChord=c)
2.0
'''
if thisChord:
return thisChord.duration.quarterLength
return e.duration.quarterLength
def _hashRoundedDuration(self, e, thisChord=None):
'''
TODO: Check if this is working
'''
if thisChord:
return self._getApproxDurOrOffset(float(thisChord.duration.quarterLength))
e.duration.quarterLength = self._getApproxDurOrOffset(float(e.duration.quarterLength))
return e.duration.quarterLength
def _hashMIDIPitchName(self, e, thisChord=None):
'''
returns midi pitch value (21-108) of a note
returns 0 if rest
returns 1 if not hashing individual notes of a chord
>>> n = note.Note(72)
>>> c = chord.Chord('A-4 C#5 E5')
>>> h = alpha.analysis.hasher.Hasher()
>>> h.hashChordsAsChords = True
>>> h._hashMIDIPitchName(n, thisChord=c)
1
>>> h.hashChordsAsChords = False
>>> h._hashMIDIPitchName(n, thisChord=c)
72
>>> r = note.Rest()
>>> h._hashMIDIPitchName(r, thisChord=c)
0
'''
if thisChord and self.hashChordsAsChords:
return 1
elif isinstance(e, note.Rest):
return 0
return e.pitch.midi
def _hashPitchName(self, e, thisChord=None):
'''
returns string representation of a note e.g. 'F##4'
returns 'r' if rest
returns 'z' if not hashing individual notes of a chord (i.e. hashing chords as chords)
>>> n = note.Note(72)
>>> c = chord.Chord('A-4 C#5 E5')
>>> h = alpha.analysis.hasher.Hasher()
>>> h.hashChordsAsChords = True
>>> h._hashPitchName(n, thisChord=c)
'z'
>>> h.hashChordsAsChords = False
>>> h._hashPitchName(n, thisChord=c)
'C5'
>>> r = note.Rest()
>>> h._hashPitchName(r, thisChord=c)
'r'
'''
if thisChord and self.hashChordsAsChords:
return 'z'
elif isinstance(e, note.Rest):
return 'r'
return str(e.pitch)
def _hashPitchNameNoOctave(self, e, thisChord=None):
'''
returns string representation of a note without the octave e.g. 'F##'
returns 'r' if rest
returns 'z' if not hashing individual notes of a chord
>>> n = note.Note(72)
>>> c = chord.Chord('A-4 C#5 E5')
>>> h = alpha.analysis.hasher.Hasher()
>>> h.hashChordsAsChords = True
>>> h._hashPitchNameNoOctave(n, thisChord=c)
'z'
>>> h.hashChordsAsChords = False
>>> h._hashPitchNameNoOctave(n, thisChord=c)
'C'
>>> r = note.Rest()
>>> h._hashPitchNameNoOctave(r, thisChord=c)
'r'
'''
if thisChord and self.hashChordsAsChords:
return 'z'
elif isinstance(e, note.Rest):
return 'r'
return str(e.pitch)[:-1]
def _hashOctave(self, e, thisChord=None):
'''
returns octave number of a note
returns -1 if rest or not hashing individual notes of a chord
>>> n = note.Note(72)
>>> c = chord.Chord('A-4 C#5 E5')
>>> h = alpha.analysis.hasher.Hasher()
>>> h.hashChordsAsChords = True
>>> h._hashOctave(c, thisChord=c)
-1
>>> h.hashChordsAsChords = False
>>> h._hashOctave(n, thisChord=c)
5
>>> r = note.Rest()
>>> h._hashOctave(r, thisChord=c)
-1
'''
if isinstance(e, chord.Chord) and self.hashChordsAsChords:
return -1
elif isinstance(e, note.Rest):
return -1
return e.octave
def _hashIsAccidental(self, e, thisChord=None):
# TODO: figure out how to tell if note is accidental based on key sig
pass
def _hashRoundedOffset(self, e, thisChord=None):
'''
returns offset rounded to the nearest subdivided beat
subdivided beat is indicated with self.granularity
by default, the granularity is set to 32, or 32nd notes
'''
if thisChord:
return self._getApproxDurOrOffset(thisChord.offset)
e.offset = self._getApproxDurOrOffset(e.offset)
return e.offset
def _hashOffset(self, e, thisChord=None):
'''
returns unrounded floating point representation of a note's offset
'''
if thisChord:
return thisChord.offset
return e.offset
def _hashIntervalFromLastNote(self, e, thisChord=None):
'''
returns the interval between last note and current note, if extant
known issues with first note of every measure in transposed pieces
returns 0 if things don't work
'''
try:
if (isinstance(e, note.Note)
and e.previous('Note') is not None):
previousNote = e.previous('Note')
if previousNote is None:
return 0
intFromLastNote = interval.Interval(noteStart=previousNote,
noteEnd=e).intervalClass
return interval.convertGeneric(interval.Interval(intFromLastNote).intervalClass)
except TypeError:
return 0
def _hashPrimeFormString(self, e, thisChord=None):
'''
returns prime form of a chord as a string e.g. '<037>'
returns '<>' otherwise
'''
if thisChord:
return thisChord.primeFormString
return '<>'
def _hashChordNormalOrderString(self, e, thisChord=None):
'''
returns normal order of a chord as a string e.g. '<047>'
returns '<>' otherwise
'''
if thisChord:
return thisChord.formatVectorString(thisChord.normalOrder)
return '<>'
# --- End Individual Hashing Functions
def setupTupleList(self):
'''
Sets up self.hashingFunctions, a dictionary of which properties of self.validTypes should
be hashed and which hashing functions should be used for those properties. Creates a
tupleList of all the properties that are hashed and uses that to create a named tuple
NoteHash with those properties. This is how we can generate a malleable named tuple
NoteHash that is different depending upon which properties a particular instance of
Hasher object hashes.
'''
tupleList = []
if self.hashPitch:
tupleList.append('Pitch')
if self.hashMIDI:
self.hashingFunctions['Pitch'] = self._hashMIDIPitchName
elif not self.hashMIDI and not self.hashNoteNameOctave:
self.hashingFunctions['Pitch'] = self._hashPitchName
elif not self.hashMIDI and self.hashNoteNameOctave:
self.hashingFunctions['Pitch'] = self._hashPitchNameNoOctave
if self.hashIsAccidental:
tupleList.append('IsAccidental')
self.hashingFunctions['IsAccidental'] = self._hashIsAccidental
if self.hashOctave:
tupleList.append('Octave')
self.hashingFunctions['Octave'] = self._hashOctave
if self.hashChordsAsNotes:
pass
elif self.hashChordsAsChords:
if self.hashNormalOrderString:
tupleList.append('NormalOrderString')
self.hashingFunctions['NormalOrderString'] = self._hashChordNormalOrderString
if self.hashPrimeFormString:
tupleList.append('PrimeFormString')
self.hashingFunctions['PrimeFormString'] = self._hashPrimeFormString
if self.hashDuration:
tupleList.append('Duration')
if self.roundDurationAndOffset:
self.hashingFunctions['Duration'] = self._hashRoundedDuration
else:
self.hashingFunctions['Duration'] = self._hashDuration
if self.hashOffset:
tupleList.append('Offset')
if self.roundDurationAndOffset:
self.hashingFunctions['Offset'] = self._hashRoundedOffset
else:
self.hashingFunctions['Offset'] = self._hashOffset
if self.hashIntervalFromLastNote:
tupleList.append('IntervalFromLastNote')
self.hashingFunctions['IntervalFromLastNote'] = self._hashIntervalFromLastNote
self.tupleList = tupleList
self.tupleClass = collections.namedtuple('NoteHash', tupleList)
def preprocessStream(self, s):
'''
strips ties from stream is self.stripTies is True
'''
if self.stripTies:
try:
st = s.stripTies()
return st.recurse()
except StreamException:
return s.recurse()
return s.recurse()
def hashMeasures(self, s):
'''
lightweight hasher. only hashes number of notes, first and last pitch
'''
def hashStream(self, s):
'''
This method is the meat of the program. It goes through all the elements that are left
to be hashed and individually hashes them by looking up which hashing functions ought
to be used on each element and passing off the element to the method
self.addSingleNoteHashToFinalHash, which creates the appropriate hash for that element
and adds it to self.finalHash
'''
finalHash = []
self.setupValidTypesAndStateVars()
# note emily, this creates a deep copy of the stream
ss = self.preprocessStream(s)
tupValidTypes = tuple(self.validTypes)
finalEltsToBeHashed = [elt for elt in ss if isinstance(elt, tupValidTypes)]
self.setupTupleList()
# TODO: see if can break for loop up into separate functions
for elt in finalEltsToBeHashed:
if self.hashIsAccidental and isinstance(elt, key.KeySignature):
self.stateVars['currKeySig'] = elt
elif isinstance(elt, chord.Chord):
if self.hashChordsAsNotes:
for n in elt:
singleNoteHash = [self.hashingFunctions[hashProperty](n, thisChord=elt)
for hashProperty in self.tupleList]
self.addHashToFinalHash(singleNoteHash, finalHash, n)
elif self.hashChordsAsChords:
singleNoteHash = [self.hashingFunctions[hashProperty](None, thisChord=elt)
for hashProperty in self.tupleList]
self.addHashToFinalHash(singleNoteHash, finalHash, elt)
else:
singleNoteHash = [self.hashingFunctions[hashProperty](elt)
for hashProperty in self.tupleList]
self.addHashToFinalHash(singleNoteHash, finalHash, elt)
# TODO: don't finalHash back and forth, return it in the smaller functions
return finalHash
def addHashToFinalHash(self, singleNoteHash, finalHash, reference):
tupleHash = (self.tupleClass._make(singleNoteHash))
if self.includeReference:
self.addNoteHashWithReferenceToFinalHash(finalHash, tupleHash, reference)
else:
self.addNoteHashToFinalHash(finalHash, tupleHash)
def addNoteHashWithReferenceToFinalHash(self, finalHash, tupleHash, reference):
'''
creates a NoteHashWithReference object from tupleHash and with the reference pass in
and adds the NoteHashWithReference object to the end of finalHash
If there is no rootDerivation of the reference, then just use the top level derivation
>>> from collections import namedtuple
>>> n = note.Note('C4')
>>> NoteHash = namedtuple('NoteHash', ['Pitch', 'Duration'])
>>> nh = NoteHash(n.pitch, n.duration)
>>> finalHash = []
>>> h = alpha.analysis.hasher.Hasher()
>>> h.addNoteHashWithReferenceToFinalHash(finalHash, nh, n)
>>> finalHash
[NoteHashWithReference(Pitch=C4, Duration=<music21.duration.Duration 1.0>)]
>>> finalHash[0].reference.id == n.id
True
'''
nhwr = NoteHashWithReference(tupleHash)
if reference.derivation.rootDerivation is not None:
nhwr.reference = reference.derivation.rootDerivation
else:
nhwr.reference = reference
finalHash.append(nhwr)
def addNoteHashToFinalHash(self, finalHash, tupleHash):
'''
creates a NoteHash object from tupleHash and adds the NoteHash
object to the end of finalHash
>>> from collections import namedtuple
>>> n = note.Note('C4')
>>> NoteHash = namedtuple('NoteHash', ['Pitch', 'Duration'])
>>> nh = NoteHash(n.pitch, n.duration)
>>> finalHash = []
>>> h = alpha.analysis.hasher.Hasher()
>>> h.addNoteHashToFinalHash(finalHash, nh)
>>> finalHash
[(<music21.pitch.Pitch C4>, <music21.duration.Duration 1.0>)]
'''
nh = NoteHash(tupleHash)
finalHash.append(nh)
# def addSingleNoteHashToFinalHash(self, singleNoteHash, finalHash, reference=None):
# # TODO: use the linter, reference DOESN'T have to be passed in
# # what is reference? it's a hashable music21 elt, write documentation
# tupleHash = (self.tupleClass._make(singleNoteHash))
# if self.includeReference:
# nhwr = NoteHashWithReference(tupleHash)
# if reference.derivation.rootDerivation is not None:
# nhwr.reference = reference.derivation.rootDerivation
# else:
# nhwr.reference = reference
# finalHash.append(nhwr)
# else:
# nh = NoteHash(tupleHash)
# finalHash.append(nh)
# --- Begin Rounding Helper Functions ---
def _getApproxDurOrOffset(self, durOrOffset):
return round(durOrOffset * self.granularity) / self.granularity
def _approximatelyEqual(self, a, b, sig_fig=4):
'''
use to look at whether beat lengths are close, within a certain range
probably can use for other things that are approx. equal
'''
return a == b or int(a * 10 ** sig_fig) == int(b * 10 ** sig_fig)
# --- End Rounding Helper Functions ---
class NoteHashWithReference:
'''
returns tuple with reference to original note or chord or rest
>>> from collections import namedtuple
>>> NoteHash = namedtuple('NoteHash', ['Pitch', 'Duration'])
>>> nh = NoteHash(60, 4)
>>> nhwr = alpha.analysis.hasher.NoteHashWithReference(nh)
>>> nhwr.reference = note.Note('C4')
>>> nhwr
NoteHashWithReference(Pitch=60, Duration=4)
>>> nhwr.Pitch
60
>>> nhwr.Duration
4
>>> nhwr.hashItemsKeys
('Pitch', 'Duration')
>>> for val in nhwr:
... print(val)
60
4
>>> nhwr.reference
<music21.note.Note C>
'''
def __init__(self, hashItemsNT):
self.reference = None
hashItemsDict = hashItemsNT._asdict()
for x in hashItemsDict:
setattr(self, x, hashItemsDict[x])
self.hashItemsKeys = tuple(hashItemsDict.keys())
def __iter__(self):
for keyName in self.hashItemsKeys:
yield(getattr(self, keyName))
def __repr__(self):
nhStrAll = 'NoteHashWithReference('
vals = []
for x in self.hashItemsKeys:
nhStr = x
nhStr += '='
nhStr += str(getattr(self, x))
vals.append(nhStr)
nhStrAll += ', '.join(vals)
nhStrAll += ')'
return nhStrAll
class NoteHash(tuple):
'''
>>> note1 = note.Note('C4')
>>> nh = alpha.analysis.hasher.NoteHash((1, 2))
>>> nh
(1, 2)
>>> a, b = nh
>>> a
1
>>> b
2
>>> nh.__class__
<... 'music21.alpha.analysis.hasher.NoteHash'>
'''
def __new__(cls, tupEls):
return super(NoteHash, cls).__new__(cls, tuple(tupEls))
class Test(unittest.TestCase):
def runTest(self):
pass
def _approximatelyEqual(self, a, b, sig_fig=2):
'''
use to look at whether beat lengths are close, within a certain range
probably can use for other things that are approx. equal
'''
return (a == b or int(a * 10 ** sig_fig) == int(b * 10 ** sig_fig))
def testBasicHash(self):
'''
test for hasher with basic settings: pitch, rounded duration, offset
with notes, chord, and rest
'''
s1 = stream.Stream()
note1 = note.Note('C4')
note1.duration.type = 'half'
note2 = note.Note('F#4')
note3 = note.Note('B-2')
cMinor = chord.Chord(['C4', 'G4', 'E-5'])
cMinor.duration.type = 'half'
r = note.Rest(quarterLength=1.5)
s1.append(note1)
s1.append(note2)
s1.append(note3)
s1.append(cMinor)
s1.append(r)
h = Hasher()
hashes_plain_numbers = [(60, 2.0, 0.0), (66, 1.0, 2.0), (46, 1.0, 3.0), (60, 2.0, 4.0),
(67, 2.0, 4.0), (75, 2.0, 4.0), (0, 1.5, 6.0)]
CNoteHash = collections.namedtuple('NoteHash', ['Pitch', 'Duration', 'Offset'])
hashes_in_format = [CNoteHash(Pitch=x, Duration=y, Offset=z)
for (x, y, z) in hashes_plain_numbers]
self.assertEqual(h.hashStream(s1), hashes_in_format)
def testHashChordsAsChordsPrimeFormString(self):
'''
test to make sure that hashing works when trying to hash chord as chord
'''
s1 = stream.Stream()
note1 = note.Note('C4')
note1.duration.type = 'half'
cMinor = chord.Chord(['C4', 'G4', 'E-5'])
cMinor.duration.type = 'half'
cMajor = chord.Chord(['C4', 'G4', 'E4'])
cMajor.duration.type = 'whole'
s1.append(note1)
s1.append(cMinor)
s1.append(cMajor)
h = Hasher()
h.hashChordsAsChords = True
h.hashChordsAsNotes = False
h.hashPrimeFormString = True
CNoteHash = collections.namedtuple('NoteHash', ['Pitch', 'PrimeFormString',
'Duration', 'Offset'])
hashes_plain_numbers = [(60, '<>', 2.0, 0.0), (1, '<037>', 2.0, 2.0),
(1, '<037>', 4.0, 4.0)]
hashes_in_format = [CNoteHash(Pitch=x, PrimeFormString=y, Duration=z, Offset=a)
for (x, y, z, a) in hashes_plain_numbers]
self.assertEqual(h.hashStream(s1), hashes_in_format)
def testHashChordsAsChordsNormalOrder(self):
s2 = stream.Stream()
note1 = note.Note('C4')
note1.duration.type = 'half'
cMinor = chord.Chord(['C4', 'G4', 'E-5'])
cMinor.duration.type = 'half'
cMajor = chord.Chord(['C4', 'G4', 'E3'])
cMajor.duration.type = 'whole'
s2.append(note1)
s2.append(cMinor)
s2.append(cMajor)
h = Hasher()
h.hashChordsAsChords = True
h.hashChordsAsNotes = False
h.hashPrimeFormString = False
h.hashNormalOrderString = True
CNoteHash = collections.namedtuple('NoteHash', ['Pitch', 'NormalOrderString',
'Duration', 'Offset'])
hashes_plain_numbers = [(60, '<>', 2.0, 0.0), (1, '<037>', 2.0, 2.0),
(1, '<047>', 4.0, 4.0)]
hashes_in_format = [CNoteHash(Pitch=x, NormalOrderString=y, Duration=z, Offset=a)
for (x, y, z, a) in hashes_plain_numbers]
self.assertEqual(h.hashStream(s2), hashes_in_format)
def testHashUnroundedDuration(self):
s3 = stream.Stream()
note1 = note.Note('C4')
note2 = note.Note('G4')
cMinor = chord.Chord(['C4', 'G4'])
note1.duration.quarterLength = 1.783
note2.duration.quarterLength = 2 / 3
cMinor.duration.type = 'half'
s3.append(note1)
s3.append(note2)
s3.append(cMinor)
h = Hasher()
h.roundDurationAndOffset = False
CNoteHash = collections.namedtuple('NoteHash', ['Pitch', 'Duration', 'Offset'])
hashes_plain_numbers = [(60, 1.783, 0.0), (67, 2 / 3, 1.783), (60, 2.0, 1.783 + 2 / 3),
(67, 2.0, 1.783 + 2 / 3)]
hashes_in_format = [CNoteHash(Pitch=x, Duration=z, Offset=a)
for (x, z, a) in hashes_plain_numbers]
h3 = h.hashStream(s3)
h3_floats = [h3[0][2], h3[1][2], h3[2][2], h3[3][2]]
answers_floats = [hashes_in_format[0][2],
hashes_in_format[1][2],
hashes_in_format[2][2],
hashes_in_format[3][2]]
assert all(self._approximatelyEqual(*values)
for values in zip(h3_floats, answers_floats))
def testHashRoundedDuration(self):
s3 = stream.Stream()
note1 = note.Note('C4')
note2 = note.Note('G4')
cMinor = chord.Chord(['C4', 'G4'])
note1.duration.quarterLength = 1.783
note2.duration.quarterLength = 2 / 3
cMinor.duration.type = 'half'
s3.append(note1)
s3.append(note2)
s3.append(cMinor)
h = Hasher()
h.roundDurationAndOffset = True
CNoteHash = collections.namedtuple('NoteHash', ['Pitch', 'Duration', 'Offset'])
hashes_plain_numbers = [(60, 1.78125, 0.0), (67, 0.65625, 1.78125), (60, 2.0, 2.4375),
(67, 2.0, 2.4375)]
hashes_in_format = [CNoteHash(Pitch=x, Duration=z, Offset=a)
for (x, z, a) in hashes_plain_numbers]
h3 = h.hashStream(s3)
self.assertEqual(h3, hashes_in_format)
h.granularity = 8 # smallest length note is now 8th note
new_hashes_in_format = [(60, 1.75, 0.0),
(67, 0.625, 1.75),
(60, 2.0, 2.5),
(67, 2.0, 2.5)]
h4 = h.hashStream(s3)
self.assertEqual(h4, new_hashes_in_format)
def testReferences(self):
s = stream.Stream()
note1 = note.Note('C4')
note2 = note.Note('G4')
s.append([note1, note2])
h = Hasher()
h.includeReference = True
hashes = h.hashStream(s)
note1ref = hashes[0].reference
note2ref = hashes[1].reference
self.assertEqual(note1.id, note1ref.id)
self.assertEqual(note2.id, note2ref.id)
def testIntervals(self):
s = stream.Stream()
note1 = note.Note('E5')
note2 = note.Note('D5')
note3 = note.Note('A5')
s.append([note1, note2, note3])
h = Hasher()
h.hashPitch = True
h.hashDuration = False
h.hashOffset = False
h.hashIntervalFromLastNote = True
unused_hashes = h.hashStream(s)
class TestExternal(unittest.TestCase):
def runTest(self):
pass
# def testBasicHash(self):
# # from pprint import pprint as pp
# from music21 import corpus
# s1 = corpus.parse('schoenberg', 6).parts[0]
# h = Hasher()
# # h.hashPitch = True
# # h.hashDuration = True
# # h.hashOffset = True
# # h.hashMIDI = False
# # h.hashChords = False
# # h.hashChordsAsNotes = False
# # h.validTypes = [note.Note, note.Rest]
# # h.hashMIDI = False # otherwise, hash string 'C-- instead of 58'
# # h.hashOctave = False
# # h.hashDuration = True
# # h.roundDurationAndOffset = False
# # h.roundOffset = False
# # h.hashChordsAsNotes = False
# # h.hashChordsAsChords = True
# # h.hashOctave = True
# # h.hashPrimeiFormString = True
# h.hashIntervalFromLastNote = True
# # pp(h.hashStream(s1.recurse()))
# # hashes1 = h.hashStream(s1.recurse())
# s2 = corpus.parse('schoenberg', 2).parts[0]
# # hashes2 = h.hashStream(s2.recurse())
# s3 = corpus.parse('bwv66.6').parts[0]
# # print(type(s3.recurse()))
# hashes3 = h.hashStream(s3)
# # s4 = corpus.parse('bwv66.6').parts[0].transpose('M2')
# # s4 = s5.parts[0].transpose('M2')
# s4.show()
# # pp(s4.recurse())
# hashes4 = h.hashStream(s4)
# print(hashes3)
# print(' ')
# print(hashes4)
# pp(difflib.SequenceMatcher(a=hashes1, b=hashes2).ratio())
# pp(difflib.SequenceMatcher(a=hashes1, b=hashes3).ratio())
# pp(difflib.SequenceMatcher(a=hashes2, b=hashes3).ratio())
# # pp(difflib.SequenceMatcher(a=hashes3, b=hashes4).ratio())
# def testfolk(self):
# from music21 import corpus
# h = Hasher()
# s1 = corpus.parse('ryansMammoth/MyLoveIsInAmericaReel.abc').parts[0]
# s2 = corpus.parse('ryansMammoth/MyLoveIsFarAwayReel.abc').parts[0]
# s2.show()
# hashes1 = h.hashStream(s1)
# hashes2 = h.hashStream(s2)
# print(difflib.SequenceMatcher(a=hashes1, b=hashes2).ratio())
# h.hashPitch = False
# hashes1 = h.hashStream(s1)
# hashes2 = h.hashStream(s2)
# print(difflib.SequenceMatcher(a=hashes1, b=hashes2).ratio())
def testBvSvS(self):
from music21 import corpus
h = Hasher()
h.hashDuration = False
h.hashOffset = False
s1 = corpus.parse('schoenberg', 6).parts[0]
s2 = corpus.parse('schoenberg', 2).parts[0]
s3 = corpus.parse('bwv66.6').parts[0]
hashes1 = h.hashStream(s1)
hashes2 = h.hashStream(s2)
hashes3 = h.hashStream(s3)
print(difflib.SequenceMatcher(a=hashes1, b=hashes2).ratio())
print(difflib.SequenceMatcher(a=hashes1, b=hashes3).ratio())
print(difflib.SequenceMatcher(a=hashes2, b=hashes3).ratio())
s2.show()
h.hashPitch = False
h.hashDuration = True
h.hashOffset = True
hashes1 = h.hashStream(s1)
hashes2 = h.hashStream(s2)
hashes3 = h.hashStream(s3)
print(difflib.SequenceMatcher(a=hashes1, b=hashes2).ratio())
print(difflib.SequenceMatcher(a=hashes1, b=hashes3).ratio())
print(difflib.SequenceMatcher(a=hashes2, b=hashes3).ratio())
def testInterval(self):
from music21 import corpus
h = Hasher()
s3 = corpus.parse('bwv66.6').parts[0]
s4 = corpus.parse('bwv66.6').parts[0].transpose('M2')
hashes3 = h.hashStream(s3)
hashes4 = h.hashStream(s4)
print(difflib.SequenceMatcher(a=hashes3, b=hashes4).ratio())
h.hashIntervalFromLastNote = True
h.hashPitch = False
hashes3 = h.hashStream(s3)
hashes4 = h.hashStream(s4)
print(difflib.SequenceMatcher(a=hashes3, b=hashes4).ratio())
if __name__ == '__main__':
import music21
music21.mainTest(Test)