<s> import os , sys , mlpy , shutil , struct , simplejson <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from scipy . spatial import distance <EOL> from pylab import * <EOL> import ntpath <EOL> import audioFeatureExtraction as aF <EOL> import audioTrainTest as aT <EOL> from sklearn . lda import LDA <EOL> def generateColorMap ( ) : <EOL> '''<STR_LIT>''' <EOL> Map = cm . jet ( np . arange ( <NUM_LIT> ) ) <EOL> stringColors = [ ] <EOL> for i in range ( Map . shape [ <NUM_LIT:0> ] ) : <EOL> rgb = ( int ( <NUM_LIT:255> * Map [ i ] [ <NUM_LIT:0> ] ) , int ( <NUM_LIT:255> * Map [ i ] [ <NUM_LIT:1> ] ) , int ( <NUM_LIT:255> * Map [ i ] [ <NUM_LIT:2> ] ) ) <EOL> stringColors . append ( struct . pack ( '<STR_LIT>' , * rgb ) . encode ( '<STR_LIT>' ) ) <EOL> return stringColors <EOL> def levenshtein ( str1 , s2 ) : <EOL> '''<STR_LIT>''' <EOL> N1 = len ( str1 ) <EOL> N2 = len ( s2 ) <EOL> stringRange = [ range ( N1 + <NUM_LIT:1> ) ] * ( N2 + <NUM_LIT:1> ) <EOL> for i in range ( N2 + <NUM_LIT:1> ) : <EOL> stringRange [ i ] = range ( i , i + N1 + <NUM_LIT:1> ) <EOL> for i in range ( <NUM_LIT:0> , N2 ) : <EOL> for j in range ( <NUM_LIT:0> , N1 ) : <EOL> if str1 [ j ] == s2 [ i ] : <EOL> stringRange [ i + <NUM_LIT:1> ] [ j + <NUM_LIT:1> ] = min ( stringRange [ i + <NUM_LIT:1> ] [ j ] + <NUM_LIT:1> , stringRange [ i ] [ j + <NUM_LIT:1> ] + <NUM_LIT:1> , stringRange [ i ] [ j ] ) <EOL> else : <EOL> stringRange [ i + <NUM_LIT:1> ] [ j + <NUM_LIT:1> ] = min ( stringRange [ i + <NUM_LIT:1> ] [ j ] + <NUM_LIT:1> , stringRange [ i ] [ j + <NUM_LIT:1> ] + <NUM_LIT:1> , stringRange [ i ] [ j ] + <NUM_LIT:1> ) <EOL> return stringRange [ N2 ] [ N1 ] <EOL> def textListToColors ( names ) : <EOL> '''<STR_LIT>''' <EOL> Dnames = np . zeros ( ( len ( names ) , len ( names ) ) ) <EOL> for i in range ( len ( names ) ) : <EOL> for j in range ( len ( names ) ) : <EOL> Dnames [ i , j ] = <NUM_LIT:1> - <NUM_LIT> * levenshtein ( names [ i ] , names [ j ] ) / float ( len ( names [ i ] + names [ j ] ) ) <EOL> pca = mlpy . PCA ( method = '<STR_LIT>' ) <EOL> pca . learn ( Dnames ) <EOL> coeff = pca . coeff ( ) <EOL> textToColor = pca . transform ( Dnames , k = <NUM_LIT:1> ) <EOL> textToColor = <NUM_LIT:255> * ( textToColor - textToColor . min ( ) ) / ( textToColor . max ( ) - textToColor . min ( ) ) <EOL> textmaps = generateColorMap ( ) ; <EOL> colors = [ textmaps [ int ( c ) ] for c in textToColor ] <EOL> return colors <EOL> def textListToColorsSimple ( names ) : <EOL> '''<STR_LIT>''' <EOL> uNames = list ( set ( names ) ) <EOL> uNames . sort ( ) <EOL> textToColor = [ uNames . index ( n ) for n in names ] <EOL> textToColor = np . array ( textToColor ) <EOL> textToColor = <NUM_LIT:255> * ( textToColor - textToColor . min ( ) ) / ( textToColor . max ( ) - textToColor . min ( ) ) <EOL> textmaps = generateColorMap ( ) ; <EOL> colors = [ textmaps [ int ( c ) ] for c in textToColor ] <EOL> return colors <EOL> def chordialDiagram ( fileStr , SM , Threshold , names , namesCategories ) : <EOL> '''<STR_LIT>''' <EOL> colors = textListToColorsSimple ( namesCategories ) <EOL> SM2 = SM . copy ( ) <EOL> SM2 = ( SM2 + SM2 . T ) / <NUM_LIT> <EOL> for i in range ( SM2 . shape [ <NUM_LIT:0> ] ) : <EOL> M = Threshold <EOL> SM2 [ i , SM2 [ i , : ] < M ] = <NUM_LIT:0> ; <EOL> dirChordial = fileStr + "<STR_LIT>" <EOL> if not os . path . isdir ( dirChordial ) : <EOL> os . mkdir ( dirChordial ) <EOL> jsonPath = dirChordial + os . sep + "<STR_LIT>" <EOL> namesPath = dirChordial + os . sep + "<STR_LIT>" <EOL> jsonSMMatrix = simplejson . dumps ( SM2 . tolist ( ) ) <EOL> f = open ( jsonPath , '<STR_LIT:w>' ) ; f . write ( jsonSMMatrix ) ; f . close ( ) <EOL> f = open ( namesPath , '<STR_LIT:w>' ) ; f . write ( "<STR_LIT>" ) ; <EOL> for i , n in enumerate ( names ) : <EOL> f . write ( "<STR_LIT>" . format ( n , "<STR_LIT:#>" + colors [ i ] ) ) <EOL> f . close ( ) <EOL> shutil . copyfile ( "<STR_LIT>" , dirChordial + os . sep + "<STR_LIT>" ) <EOL> shutil . copyfile ( "<STR_LIT>" , dirChordial + os . sep + "<STR_LIT>" ) <EOL> def visualizeFeaturesFolder ( folder , dimReductionMethod , priorKnowledge = "<STR_LIT:none>" ) : <EOL> '''<STR_LIT>''' <EOL> if dimReductionMethod == "<STR_LIT>" : <EOL> allMtFeatures , wavFilesList = aF . dirWavFeatureExtraction ( folder , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , computeBEAT = True ) <EOL> if allMtFeatures . shape [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> return <EOL> namesCategoryToVisualize = [ ntpath . basename ( w ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] for w in wavFilesList ] ; <EOL> namesToVisualize = [ ntpath . basename ( w ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) for w in wavFilesList ] ; <EOL> ( F , MEAN , STD ) = aT . normalizeFeatures ( [ allMtFeatures ] ) <EOL> F = np . concatenate ( F ) <EOL> pca = mlpy . PCA ( method = '<STR_LIT>' ) <EOL> pca . learn ( F ) <EOL> coeff = pca . coeff ( ) <EOL> K1 = <NUM_LIT:2> <EOL> K2 = <NUM_LIT:10> <EOL> if K1 > F . shape [ <NUM_LIT:0> ] : <EOL> K1 = F . shape [ <NUM_LIT:0> ] <EOL> if K2 > F . shape [ <NUM_LIT:0> ] : <EOL> K2 = F . shape [ <NUM_LIT:0> ] <EOL> finalDims = pca . transform ( F , k = K1 ) <EOL> finalDims2 = pca . transform ( F , k = K2 ) <EOL> else : <EOL> allMtFeatures , Ys , wavFilesList = aF . dirWavFeatureExtractionNoAveraging ( folder , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> if allMtFeatures . shape [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> return <EOL> namesCategoryToVisualize = [ ntpath . basename ( w ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] for w in wavFilesList ] ; <EOL> namesToVisualize = [ ntpath . basename ( w ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) for w in wavFilesList ] ; <EOL> ldaLabels = Ys <EOL> if priorKnowledge == "<STR_LIT>" : <EOL> uNamesCategoryToVisualize = list ( set ( namesCategoryToVisualize ) ) <EOL> YsNew = np . zeros ( Ys . shape ) <EOL> for i , uname in enumerate ( uNamesCategoryToVisualize ) : <EOL> indicesUCategories = [ j for j , x in enumerate ( namesCategoryToVisualize ) if x == uname ] <EOL> for j in indicesUCategories : <EOL> indices = np . nonzero ( Ys == j ) <EOL> YsNew [ indices ] = i <EOL> ldaLabels = YsNew <EOL> ( F , MEAN , STD ) = aT . normalizeFeatures ( [ allMtFeatures ] ) <EOL> F = np . array ( F [ <NUM_LIT:0> ] ) <EOL> clf = LDA ( n_components = <NUM_LIT:10> ) <EOL> clf . fit ( F , ldaLabels ) <EOL> reducedDims = clf . transform ( F ) <EOL> pca = mlpy . PCA ( method = '<STR_LIT>' ) <EOL> pca . learn ( reducedDims ) <EOL> coeff = pca . coeff ( ) <EOL> reducedDims = pca . transform ( reducedDims , k = <NUM_LIT:2> ) <EOL> uLabels = np . sort ( np . unique ( ( Ys ) ) ) <EOL> reducedDimsAvg = np . zeros ( ( uLabels . shape [ <NUM_LIT:0> ] , reducedDims . shape [ <NUM_LIT:1> ] ) ) <EOL> finalDims = np . zeros ( ( uLabels . shape [ <NUM_LIT:0> ] , <NUM_LIT:2> ) ) <EOL> for i , u in enumerate ( uLabels ) : <EOL> indices = [ j for j , x in enumerate ( Ys ) if x == u ] <EOL> f = reducedDims [ indices , : ] <EOL> finalDims [ i , : ] = f . mean ( axis = <NUM_LIT:0> ) <EOL> finalDims2 = reducedDims <EOL> for i in range ( finalDims . shape [ <NUM_LIT:0> ] ) : <EOL> plt . text ( finalDims [ i , <NUM_LIT:0> ] , finalDims [ i , <NUM_LIT:1> ] , ntpath . basename ( wavFilesList [ i ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) , horizontalalignment = '<STR_LIT>' , verticalalignment = '<STR_LIT>' , fontsize = <NUM_LIT:10> ) <EOL> plt . plot ( finalDims [ i , <NUM_LIT:0> ] , finalDims [ i , <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> plt . xlim ( [ <NUM_LIT> * finalDims [ : , <NUM_LIT:0> ] . min ( ) , <NUM_LIT> * finalDims [ : , <NUM_LIT:0> ] . max ( ) ] ) <EOL> plt . ylim ( [ <NUM_LIT> * finalDims [ : , <NUM_LIT:1> ] . min ( ) , <NUM_LIT> * finalDims [ : , <NUM_LIT:1> ] . max ( ) ] ) <EOL> plt . show ( ) <EOL> SM = <NUM_LIT:1.0> - distance . squareform ( distance . pdist ( finalDims2 , '<STR_LIT>' ) ) <EOL> for i in range ( SM . shape [ <NUM_LIT:0> ] ) : <EOL> SM [ i , i ] = <NUM_LIT:0.0> ; <EOL> chordialDiagram ( "<STR_LIT>" , SM , <NUM_LIT> , namesToVisualize , namesCategoryToVisualize ) <EOL> SM = <NUM_LIT:1.0> - distance . squareform ( distance . pdist ( F , '<STR_LIT>' ) ) <EOL> for i in range ( SM . shape [ <NUM_LIT:0> ] ) : <EOL> SM [ i , i ] = <NUM_LIT:0.0> ; <EOL> chordialDiagram ( "<STR_LIT>" , SM , <NUM_LIT> , namesToVisualize , namesCategoryToVisualize ) <EOL> uNamesCategoryToVisualize = sort ( list ( set ( namesCategoryToVisualize ) ) ) <EOL> finalDimsGroup = np . zeros ( ( len ( uNamesCategoryToVisualize ) , finalDims2 . shape [ <NUM_LIT:1> ] ) ) <EOL> for i , uname in enumerate ( uNamesCategoryToVisualize ) : <EOL> indices = [ j for j , x in enumerate ( namesCategoryToVisualize ) if x == uname ] <EOL> f = finalDims2 [ indices , : ] <EOL> finalDimsGroup [ i , : ] = f . mean ( axis = <NUM_LIT:0> ) <EOL> SMgroup = <NUM_LIT:1.0> - distance . squareform ( distance . pdist ( finalDimsGroup , '<STR_LIT>' ) ) <EOL> for i in range ( SMgroup . shape [ <NUM_LIT:0> ] ) : <EOL> SMgroup [ i , i ] = <NUM_LIT:0.0> ; <EOL> chordialDiagram ( "<STR_LIT>" , SMgroup , <NUM_LIT> , uNamesCategoryToVisualize , uNamesCategoryToVisualize ) </s>
