<s> import util <EOL> import chordKMeans <EOL> import sys <EOL> import numpy as np <EOL> import matplotlib <EOL> import matplotlib . pyplot as plt <EOL> import pylab <EOL> import random <EOL> beatsPerBarDefault = <NUM_LIT:4> <EOL> kMeansDefault = <NUM_LIT:7> <EOL> def generateAndWriteCentroids ( midiFiles , numCentroids = kMeansDefault , beatsPerBar = beatsPerBarDefault , fileName = None ) : <EOL> featureCentroids , centroidPoints = chordKMeans . getFeatureCentroids ( midiFiles , numCentroids = numCentroids , beatsPerBar = beatsPerBar ) <EOL> if not fileName : <EOL> fileName = '<STR_LIT>' . join ( [ random . choice ( '<STR_LIT>' ) for _ in range ( <NUM_LIT:10> ) ] ) + '<STR_LIT>' <EOL> print '<STR_LIT>' , fileName <EOL> with open ( fileName , '<STR_LIT:w>' ) as f : <EOL> for arr in featureCentroids : <EOL> f . write ( '<STR_LIT:U+0020>' . join ( map ( str , arr . tolist ( ) ) ) ) <EOL> f . write ( '<STR_LIT:\n>' ) <EOL> return fileName <EOL> def readCentroids ( fileName ) : <EOL> with open ( fileName , '<STR_LIT:r>' ) as f : <EOL> mat = [ ] <EOL> for l in f : <EOL> arr = [ ] <EOL> for x in l . split ( ) : <EOL> arr . append ( float ( x ) ) <EOL> mat . append ( arr ) <EOL> return np . matrix ( mat ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if len ( sys . argv ) <= <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> filename = sys . argv [ <NUM_LIT:1> ] + "<STR_LIT>" <EOL> midiFiles = sys . argv [ <NUM_LIT:2> : ] <EOL> num_reps = <NUM_LIT:5> <EOL> centers = [ chordKMeans . getFeatureCentroids ( midiFiles , numCentroids = kMeansDefault , beatsPerBar = beatsPerBarDefault ) for _ in range ( num_reps ) ] <EOL> results = [ chordKMeans . evaluateKmeansClusters ( midiFiles , centroids , corr_centers ) for ( centroids , corr_centers ) in centers ] <EOL> enum = [ ( e , z ) for e , z in enumerate ( results ) ] <EOL> featureCentroids = centers [ max ( enum , key = lambda x : x [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] ] [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' , filename <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> for arr in featureCentroids : <EOL> f . write ( '<STR_LIT:U+0020>' . join ( map ( str , arr . tolist ( ) ) ) ) <EOL> f . write ( '<STR_LIT:\n>' ) </s>
