annotate Functions/HashFunctions.py @ 2:46fb79167a61 tip

Main Code
author Victor Padilla <victor.padilla.mc@gmail.com>
date Mon, 04 May 2015 22:56:18 +0200
parents 0f7f611deca4
children
rev   line source
victor@1 1 '''
victor@1 2 @organization: Lancaster University & University of Leeds
victor@1 3 @version: 1.0
victor@1 4 Created on 11/12/2014
victor@1 5
victor@1 6 @author: Victor Padilla
victor@1 7 @contact: v.padilla@lancaster.ac.uk
victor@1 8
victor@1 9 Functions to manipulate and convert to Hash array
victor@1 10 the scores.
victor@1 11
victor@1 12 It is useful for aligning measures (not single notes)
victor@1 13
victor@1 14 '''
victor@1 15 from music21 import stream
victor@1 16 from music21 import note
victor@1 17 from music21 import omr
victor@1 18 # FastAlignmentArrays is a Cython library
victor@1 19 from Alignment import FastAlignmentArrays
victor@1 20 from MeasureFunctions import MeasureFunctions
victor@1 21
victor@1 22 class HashFunctions:
victor@1 23
victor@1 24 def alignHash(self,hashArray):
victor@1 25 '''
victor@1 26 Returns two hash arrays aligned
victor@1 27
victor@1 28 Uses the needleman-wunsh algorithm evaluating the differences between measures
victor@1 29 '''
victor@1 30 faa=FastAlignmentArrays()
victor@1 31 hashOrdered=faa.needleman_wunsch(hashArray[0], hashArray[1],False)[0]
victor@1 32 hashArray[0]=hashOrdered[0]
victor@1 33 hashArray[1]=hashOrdered[1]
victor@1 34 return hashArray
victor@1 35
victor@1 36 def getHash(self,OMR):
victor@1 37 '''
victor@1 38 Returns hash from a group of OMR parts
victor@1 39 '''
victor@1 40 print "...Obtaining Hash of measures..."
victor@1 41 hashArray=[]
victor@1 42 for i in range(len(OMR)):
victor@1 43 hashArray.append(self.getHashArrayFromPart(OMR[i].parts[0]))
victor@1 44 return hashArray
victor@1 45
victor@1 46 def getGapsFromHashArray(self,hashArray):
victor@1 47 '''
victor@1 48 Returns the gap index array from a group of hash parts
victor@1 49 '''
victor@1 50 gapsArrays=[]
victor@1 51 for i in range(len(hashArray)):
victor@1 52 gapsArray=[]
victor@1 53 for j in range(len(hashArray[i])):
victor@1 54 symbol=hashArray[i][j]
victor@1 55 if symbol=="*":
victor@1 56 gapsArray.append(j)
victor@1 57
victor@1 58 gapsArrays.append(gapsArray)
victor@1 59 return gapsArrays
victor@1 60
victor@1 61
victor@1 62
victor@1 63
victor@1 64 def reconstructHash(self,OMR,hashArray):
victor@1 65 '''
victor@1 66 Returns a group of single parts ordered by hashArray (gaps)
victor@1 67 '''
victor@1 68 scores=[]
victor@1 69 gapsArr=[]
victor@1 70 mf=MeasureFunctions()
victor@1 71 for i in range(len(OMR)):
victor@1 72 print len(hashArray[i])
victor@1 73 partReconstruct,gaps=mf.reconstructScore(OMR[i].parts[0], hashArray[i])
victor@1 74 sc=stream.Score()
victor@1 75 sc.append(partReconstruct)
victor@1 76 scores.append(sc)
victor@1 77 gapsArr.append(gaps)
victor@1 78 return scores,gapsArr
victor@1 79
victor@1 80
victor@1 81
victor@1 82
victor@1 83 def removeHashGaps(self,partHash):
victor@1 84 '''
victor@1 85 Removes gaps in a part
victor@1 86 '''
victor@1 87 newHash=[]
victor@1 88 for i in range(len(partHash)):
victor@1 89 if(partHash[i]!="*"):
victor@1 90 newHash.append(partHash[i])
victor@1 91 return newHash
victor@1 92
victor@1 93 def addHashGaps(self,partHash,gaps):
victor@1 94 '''
victor@1 95 Insert gaps in a part
victor@1 96 '''
victor@1 97 for gap in gaps:
victor@1 98 partHash.insert(gap,"")
victor@1 99 return partHash
victor@1 100
victor@1 101
victor@1 102 def getHashArrayFromPart(self,part):
victor@1 103 '''
victor@1 104 get Hash string of a Part (music21)
victor@1 105 '''
victor@1 106 hashArray=[]
victor@1 107 lengthArray=len(part.getElementsByClass(stream.Measure))
victor@1 108 for i in range(lengthArray):
victor@1 109 measure=part.getElementsByClass(stream.Measure)[i]
victor@1 110 hashMeasure=self.getHashFromMeasure(measure)
victor@1 111 pitchArr=""
victor@1 112 for mynote in measure.flat.getElementsByClass(note.Note):
victor@1 113 pitchArr+=str(mynote.pitch.name)
victor@1 114 hashArray.append(pitchArr+"_"+hashMeasure)
victor@1 115 return hashArray
victor@1 116
victor@1 117 def getHashFromMeasure(self,measure):
victor@1 118 '''
victor@1 119 get Hash string of a measure. Library correctors.py of Michael Scott Cuthbert. Project OMR
victor@1 120 '''
victor@1 121 mh=omr.correctors.MeasureHash(measure).getHashString()
victor@1 122 return mh
victor@1 123