Skip to content

Commit

Permalink
working numpy version
Browse files Browse the repository at this point in the history
  • Loading branch information
JoFrhwld committed Oct 31, 2013
1 parent 754f809 commit 7c2aba1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
18 changes: 10 additions & 8 deletions FAVE-extract/bin/extractFormants.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
import praat, esps, plotnik, cmu, vowel

FAVE_WD = os.getcwd()
import rpy2.robjects as robjects
import numpy as np
from scipy import linalg
from scipy.spatial.distance import mahalanobis as mahalanobis
import remeasure
os.chdir(FAVE_WD)

Expand Down Expand Up @@ -1051,9 +1053,9 @@ def loadCovs(inFile):
covs = {}
for line in open(inFile, 'rU').readlines():
vowel = line.strip().split('\t')[0]
values = robjects.FloatVector(line.strip().split('\t')[1:])
covs[vowel] = robjects.r['matrix'](values, nrow=4)
values = np.array([float(x) for x in line.strip().split('\t')[1:]])
covs[vowel] = np.reshape(values, (4,-1))

return covs


Expand All @@ -1063,8 +1065,8 @@ def loadMeans(inFile):
means = {}
for line in open(inFile, 'rU').readlines():
vowel = line.strip().split('\t')[0]
means[vowel] = robjects.FloatVector(line.strip().split('\t')[1:])
means[vowel] = np.array([float(x) for x in line.strip().split('\t')[1:]])

return means


Expand Down Expand Up @@ -1536,9 +1538,9 @@ def predictF1F2(phone, selectedpoles, selectedbandwidths, means, covs):
i = 0
j = 1
## vector with current pole combination and associated bandwidths
x = robjects.FloatVector([poles[i], poles[j], math.log(bandwidths[i]), math.log(bandwidths[j])])
x = np.array([poles[i], poles[j], math.log(bandwidths[i]), math.log(bandwidths[j])])
## calculate Mahalanobis distance between x and ANAE mean
dist = robjects.r['mahalanobis'](x, means[vowel], covs[vowel])[0]
dist = mahalanobis(x, means[vowel], linalg.inv(covs[vowel]))
## append poles and bandwidths to list of values
## (if F3 and bandwidth measurements exist, add to list of appended values)
if len(poles) > 2:
Expand Down
37 changes: 17 additions & 20 deletions FAVE-extract/bin/remeasure.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#import ast
import math
import rpy2.robjects as robjects
import rpy2.rinterface as rinterface
import numpy as np
from scipy import linalg
from scipy.spatial.distance import mahalanobis as mahalanobis
import sys
import string

Expand Down Expand Up @@ -119,9 +120,9 @@ def pruneVowels(vowels, vowel, vowelMeans, vowelCovs, outlie):
while not enough:
outtokens = [ ]
for token in vowels[vowel]:
x = robjects.FloatVector(token)
dist = robjects.r['mahalanobis'](x, vowelMeans[vowel], vowelCovs[vowel])[0]
if dist <= outlie:
x = np.array(token)
dist = mahalanobis(x, vowelMeans[vowel], linalg.inv(vowelCovs[vowel]))
if dist**2 <= outlie:
outtokens.append(token)
if len(outtokens) >= 10:
enough = True
Expand All @@ -144,19 +145,15 @@ def calculateVowelMeans(vowels):
vowelMeans = {}
vowelCovs = {}
for vowel in vowels:
vF1 = robjects.FloatVector([F1 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vF2 = robjects.FloatVector([F2 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vB1 = robjects.FloatVector([B1 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vB2 = robjects.FloatVector([B2 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vDur = robjects.FloatVector([Dur for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vF1 = np.array([F1 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vF2 = np.array([F2 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vB1 = np.array([B1 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vB2 = np.array([B2 for [F1,F2,B1,B2,Dur] in vowels[vowel]])
vDur = np.array([Dur for [F1,F2,B1,B2,Dur] in vowels[vowel]])

rcolMeans = robjects.r["colMeans"]
rcov = robjects.r["cov"]

measureMatrix = robjects.r["matrix"](vF1 + vF2 + vB1 + vB2 + vDur, ncol = 5)

vowelMeans[vowel] = rcolMeans(measureMatrix)
vowelCovs[vowel] = rcov(measureMatrix)
vowelMeans[vowel] = np.array([vF1.mean(), vF2.mean(), vB1.mean(), vB2.mean(), vDur.mean()])
vowelCovs[vowel] = np.cov(np.vstack((vF1, vF2, vB1, vB2, vDur)))
#sys.stderr.write("Vowel means calculated\n")
return vowelMeans, vowelCovs

Expand Down Expand Up @@ -203,7 +200,7 @@ def repredictF1F2(measurements, vowelMeans, vowelCovs, vowels):
values = [F1, F2, B1, B2, lDur]
outvalues = [F1, F2, F3, B1, B2, B3, lDur]

x = robjects.FloatVector(values)
x = np.array(values)

##If there is only one member of a vowel category,
##the covariance matrix will be filled with NAs
Expand All @@ -212,21 +209,21 @@ def repredictF1F2(measurements, vowelMeans, vowelCovs, vowels):
## if no re-measurement is to take place for one of the three reasons below, the list of candidate measurements and nFormants
## will be filled with four identical copies of the original measurement, all with a distance of zero
## so that the original measurement is guaranteed to be re-selected
if vowelCovs[vowel][0] is rinterface.NA_Real:
if np.isnan(vowelCovs[vowel][0,0]):
valuesList.append([float(vm.f1), float(vm.f2), vm.f3, math.log(float(vm.b1)), math.log(float(vm.b2)), vm.b3, lDur])
distanceList.append(0)
nFormantsList.append(vm.nFormants)
elif len(vowels[vowel]) < 7:
valuesList.append([float(vm.f1), float(vm.f2), vm.f3, math.log(float(vm.b1)), math.log(float(vm.b2)), vm.b3, lDur])
distanceList.append(0)
nFormantsList.append(vm.nFormants)
elif robjects.r['det'](vowelCovs[vowel])[0] == 0: ## determinant of the covariance matrix is zero
elif linalg.det(vowelCovs[vowel]) == 0: ## determinant of the covariance matrix is zero
valuesList.append([float(vm.f1), float(vm.f2), vm.f3, math.log(float(vm.b1)), math.log(float(vm.b2)), vm.b3, lDur])
distanceList.append(0)
nFormantsList.append(vm.nFormants)
## "real" re-measurement
else:
dist = robjects.r['mahalanobis' ](x, vowelMeans[vowel], vowelCovs[vowel])[0]
dist = mahalanobis(x, vowelMeans[vowel], linalg.inv(vowelCovs[vowel]))
valuesList.append(outvalues)
distanceList.append(dist)
nFormantsList.append(i + 3) ## these are the formant setting used, not the actual number of formants returned
Expand Down

0 comments on commit 7c2aba1

Please sign in to comment.