#NEWLINE# def preprocess( dataset ) : #NEWLINE# #INDENT# ' Pre-process the dataset to:\n        - interpolate the sample so that they all have the same length\n        - ensure a [xxxxx...yyyyyy...] layout\n        - normalize the coordinates so that max dimension = 1\n        - translate the shape so that the center is at (0,0)\n        - returns it as a numpy array\n        - cluster the samples into different allographs\n    ' #NEWLINE# sample_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# clustered_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# i = 0 #NEWLINE# for ( char , samples ) in dataset . items ( ) : #NEWLINE# #INDENT# i += 1 #NEWLINE# print ( ( '%.2f%% -- Interpolation and norlization of %d samples of <%s>...' % ( ( ( float ( i ) / len ( dataset ) ) * 100 ) , len ( samples ) , char ) ) ) #NEWLINE# for sample in norm_shape : #NEWLINE# #INDENT# sample = list ( itertools . chain ( * sample ) ) #NEWLINE# shape = interpolate_shape ( sample , NB_POINTS ) #NEWLINE# norm_shape = normalize ( shape ) #NEWLINE# if ( norm_shape is not None ) : #NEWLINE# #INDENT# sample_dict . setdefault ( char , [ ] ) . append ( norm_shape ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# print ( 'Done.\n\nClustering...' ) #NEWLINE# for ( k , v ) in sample_dict . items ( ) : #NEWLINE# #INDENT# clusters = cluster ( v ) #NEWLINE# print ( ( 'Found %s clusters for <%s>' % ( len ( clusters ) , k ) ) ) #NEWLINE# if ( len ( clusters ) in [ 0 , 1 ] ) : #NEWLINE# #INDENT# clustered_dict [ k ] = v #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# for ( i , c ) in enumerate ( clusters ) : #NEWLINE# #INDENT# clustered_dict [ ( k + str ( i ) ) ] = c #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return clustered_dict