forked from jbeliao/SLAM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SLAM3.py
732 lines (612 loc) · 27.7 KB
/
SLAM3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# -*- coding: utf-8 -*-
# SLAM : a method for the automatic Stylization and LAbelling of speech Melody
# Copyright (C) 2014 Julie BELIAO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
"""
#####################################################################
Automatic Stylizer.
#####################################################################
Takes a wavefile and a textgrid file as an input and compute the
styles of all the intervals of a desired tier with the SLAM algorithm.
PARAMETERS:
I/O:
---
* srcFile : path to the wave or PitchTier file to process
* inputTextgridFile : path to the input TextGrid file
* outputTextgridFile : path to the output TextGrid file
tiers of interest:
------------------
* speakerTier : average register of each speaker is computed
using this tier. For each different label in
this tier, we assume a different speaker, for
whom the average register is computed.
* targetTier : The tier whose intervals will be stylized using
SLAM
display & export:
-------
* displayExamples : True or False: whether or not to display examples
of stylized f0 segments
* displaySummary : True or False: whether or not to display a small
summary of the distribution of the stylizes
* exportFigures : True or False: whether or not to export the result
tonal analysis in PDF file
* exportTag : True or False: whether or not to export the tag
in TextGrid File
#####################################################################"""
# THRESHOLDS
timeStep = .001 #in seconds, step for swipe pitch analysis
voicedThreshold = 0.2 #for swipe
alpha = 1 # for register ranger estimation
# DATA PROCESSING
exclude_orphans = 1 # Excludes from analysis dubious voiced segments located at edges of target segments.
linear_regression = 1 # Bases output labels on a linear regression of F0 contour
glissando_threshold = 1 # Length of segment is taken into account in detection of pitch changes using the glissando threshold formula.
# Tiers for the speaker and the target intervals, put your own tier names
# speakerTier= 'periode'
# targetTier = 'pivot'
speakerTier = 'Utterance' # commentary.
targetTier = 'Word'
tagTier = 'Word'
#display and exportation
examplesDisplayCount = 0 #number of example plots to do. Possibly 0
exportFigures = True
exportTag = True
exportTSV = True
# Defines sized of target window
num_target_neighbors = 1 # Determines size of sliding window
limit_neighbors_to_support = 1 # Limits window to boundaries of support
#END OF PARAMETERS (don't touch below please)
#------------------------------------------------------
#imports
import sys, glob, os, re, time
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
os.chdir(sys.path[0])
from SLAM_utils import TextGrid, swipe, stylize, praatUtil, textgrid2tsv
import numpy as np
import matplotlib.backends.backend_pdf as pdfLib
import matplotlib.pylab as pl
import SLAM_utils.TextGrid as tgLib
import SLAM_utils.progress as progLib
change = stylize.input_SLAM(
"""
Current parameters are:
Tier providing units of register estimation (support) : %s
Tier providing units to stylize (target) : %s
Tier providing additional descriptive contents (tag) : %s
Exclude dubious F0 measurements at edges of target : %d (1 for True; 0 for False)
Base output labels on linear regression of F0 data : %d (1 for True; 0 for False)
Consider target length in detection of pitch changes : %d (1 for True; 0 for False)
Number of target neighbors used in local register : %d
Limit local register to same support interval : %d (1 for True; 0 for False)
Min. value of the width of tonal zone (ie. minDELTA) : %d (semitones)
Reference frequency for saliency detection : %d (ie. %s)
Number of examples to display : %d
Export result as PDF : %d (1 for True; 0 for False)
Include export tag in TextGrid : %d (1 for True; 0 for False)
Generate .tsv files from results : %d (1 for True; 0 for False)
ENTER = ok
anything+ENTER = change
""" %
(speakerTier, targetTier, tagTier, exclude_orphans, linear_regression, glissando_threshold, num_target_neighbors, limit_neighbors_to_support, stylize.minDELTA, stylize.freqRefSaliency, stylize.detMode2str(stylize.freqRefSaliency), examplesDisplayCount, exportFigures,exportTag,exportTSV))
print(change)
if len(change):
new = stylize.input_SLAM('Reference tier (empty = keep %s) : ' %
speakerTier)
if len(new): speakerTier = new
new = stylize.input_SLAM('Target tier (empty = keep %s) : ' % targetTier)
if len(new): targetTier = new
new = stylize.input_SLAM('Tag tier (empty = keep %s) : ' % tagTier)
if len(new): tagTier = new
new = stylize.input_SLAM('Generate .tsv files from results (empty = keep %d) : ' %
exportTSV)
if len(new): exportTSV = int(new)
new = stylize.input_SLAM('Exclude dubious F0 measurements at edges of target (empty = keep %d) : ' %
exclude_orphans)
if len(new): exclude_orphans = int(new)
new = stylize.input_SLAM('Base output labels on linear regression of F0 data (empty = keep %d) : ' %
linear_regression)
if len(new): linear_regression = int(new)
new = stylize.input_SLAM('Consider target length in detection of pitch changes (empty = keep %d) : ' %
glissando_threshold)
if len(new): glissando_threshold = int(new)
new = stylize.input_SLAM('Number of target neighbors used in local register (empty = keep %d) : ' %
num_target_neighbors)
if len(new): num_target_neighbors = int(new)
new = stylize.input_SLAM('Limit local register to same support interval (empty = keep %d) : ' %
limit_neighbors_to_support)
if len(new): limit_neighbors_to_support = int(new)
new = stylize.input_SLAM('Export figures in PDF file (empty = keep %d) : ' %
exportFigures)
if len(new): exportFigures = int(new)
new = stylize.input_SLAM('Export tag in TextGrid file (empty = keep %d) : ' %
exportTag)
if len(new): exportTag = int(new)
new = stylize.input_SLAM('Generate .tsv files from results (empty = keep %d) : ' %
exportTSV)
if len(new): exportTSV = int(new)
#all styles, for statistics
stylesGlo = []
stylesDynLoc = []
totalN = 0
#seperate input files into tgFiles and srcFiles
tmpFiles = glob.glob('./data/*.*')
tgFiles = []
srcFiles = []
hybirdFiles = []
while tmpFiles:
filename = tmpFiles.pop(0)
if re.search(r'\.TEXTGRID$', filename, re.IGNORECASE):
# Praat TextGrid
tgFiles.append(filename)
elif re.search(r'\.(COLLECTION|OR)$', filename, re.IGNORECASE) :
# hybrid files : Praat Collection / Analor File
srcFiles.append(filename)
else:
srcFiles.append(filename)
t1 = time.time()
tgFiles = sorted(tgFiles)
outputTextgridFiles_all = [] # will store output textgrids, for statistics and .tsv generation
def evaluateStylization(style_glo, targetTimes, deltaTargetPitchSmooth, D=3.2):
st = stylize.register2relst(style_glo, DELTA=3.2)
reconstructedPitch = []
#print(style_glo)
#print(deltaTargetPitchSmooth)
if len(st) == 2:
semitone_diff = st[1] - st[0]
iterations = len(deltaTargetPitchSmooth) - 1
step = semitone_diff / iterations
for i in range(0,len(deltaTargetPitchSmooth)):
reconstructedPitch.append(st[0] + step*i)
#print(reconstructedPitch, len(reconstructedPitch))
else:
# Beginning to saliency #
id_saliency = round((len(deltaTargetPitchSmooth)/4)*int(st[3]))
#print(id_saliency)
semitone_diff = st[2] - st[0]
iterations = id_saliency - 1
step = semitone_diff/iterations
for i in range(0, id_saliency+1):
reconstructedPitch.append(st[0] + step*i)
# Saliency to end #
semitone_diff = st[1] - st[2]
iterations = len(deltaTargetPitchSmooth) - (id_saliency+1)
step = semitone_diff/iterations
for i in range(1, iterations+1):
reconstructedPitch.append(st[2] + step*i)
differences = []
for i in range(len(reconstructedPitch)):
diff = abs(reconstructedPitch[i] - deltaTargetPitchSmooth[i])
differences.append(diff)
avg_diff = sum(differences)/len(differences)
mae = mean_absolute_error(deltaTargetPitchSmooth, reconstructedPitch)
rms = mean_squared_error(deltaTargetPitchSmooth, reconstructedPitch, squared=False)
r2 = r2_score(deltaTargetPitchSmooth, reconstructedPitch)
return avg_diff, mae, rms, r2, reconstructedPitch
#print(style_glo, deltaTargetPitchSmooth, reconstructedPitch, len(deltaTargetPitchSmooth), len(reconstructedPitch), "\n", sep="\n")
mae_all = []
rms_all = []
r2_all = []
while (tgFiles):
#take a tg file from tgFiles and its related src file(s) from SrcFiles
inputTextgridFile = tgFiles.pop(0)
basename = stylize.get_basename(inputTextgridFile)
extension = stylize.get_extension(inputTextgridFile)
outputTextgridFile = './output/{}{}'.format(basename, extension)
outputTextgridFiles_all.append(outputTextgridFile)
#outputPitchTierFile = './output/{}{}'.format(basename, ".PitchTier")
outputFigureFile = './output/{}{}'.format(basename, ".pdf")
srcFilesPaired = \
[filename for filename in srcFiles \
if stylize.get_basename(filename).lower() == basename.lower()]
for filename in srcFilesPaired:
srcFiles.remove(filename)
#Create TextGrid object
print('')
print(('Handling %s....' % basename))
print('Loading input TextGrid...')
tg = TextGrid.TextGrid()
tg.read(inputTextgridFile)
tierNames = [t.name() for t in tg]
while targetTier not in tierNames:
print(
' TextGrid does not have a tier named %s for target. Available tiers are:'
% targetTier)
for t in tierNames:
print(' %s' % t)
targetTier = stylize.input_SLAM(
'Type the tier name to use as target (+ENTER):')
while speakerTier not in tierNames and speakerTier:
print(
' TextGrid does not have a tier named %s for support. Available tiers are:'
% speakerTier)
for t in tierNames:
print(' %s' % t)
speakerTier = stylize.input_SLAM(
'Type the tier name for support (or any categorizing variable):')
while tagTier not in tierNames and tagTier:
print(
' TextGrid does not have a tier named %s for tag. Available tiers are:'
% tagTier)
for t in tierNames:
print(' %s' % t)
tagTier = stylize.input_SLAM(
'Type the tier name indicating tag Tier (or any categorizing variable):'
)
#create interval tier for output
newTier = TextGrid.IntervalTier(name='%sStyleGlo' % targetTier,
xmin=tg[targetTier].xmin(),
xmax=tg[targetTier].xmax())
newTierLoc = TextGrid.IntervalTier(name='%sStyleLoc' % targetTier,
xmin=tg[targetTier].xmin(),
xmax=tg[targetTier].xmax())
"""
Third tier here?
"""
#create a new tier called exportTag
#when the binary flag exportTag is turned on
if exportTag:
newTierTag = TextGrid.IntervalTier(name='exportTag',
xmin=tg[targetTier].xmin(),
xmax=tg[targetTier].xmax())
#Create swipe object from wave file or external PitchTier file
inputPitch = None
#try as PitchTier files (supported formats: short text and binary)
if not inputPitch:
for file in srcFilesPaired:
try:
inputPitch = stylize.readPitchtierPlus(file)
except Exception as e:
inputPitch = None
print(e)
continue
print('Reading pitch from PitchTier file {}'.format(file))
break
# try as wave files
if not inputPitch:
for file in srcFilesPaired:
if not praatUtil.isGoodMonoWav(file): continue
try:
inputPitch = swipe.Swipe(file,
pMin=75,
pMax=500,
s=timeStep,
t=voicedThreshold,
mel=False)
except:
inputPitch = None
continue
print('Computing pitch on wave file {}'.format(file))
break
# unknown format
if not inputPitch:
print('Error: source files {} are not supported !'.format(srcFilesPaired))
continue
print('Computing average register for each speaker')
"""
try:
registers = stylize.averageRegisters(inputPitch, tg[speakerTier])
except:
continue
"""
print('Stylizing each interval of the target tier')
#computing at which iterations to give progress
LEN = float(len(tg[targetTier]))
totalN += LEN
POSdisplay = set([int(float(i) / 100.0 * LEN) for i in range(0,100,10)])
smooth_total = []
time_total = []
pl.rcParams["figure.figsize"] = [13, 7]
fig = pl.figure()
support = None
haveImgInbuf = False
if exportFigures:
pdf = pdfLib.PdfPages(outputFigureFile)
prog = progLib.Progress(len(tg[targetTier]))
num_of_targets_in_support = 0
# stylize each target (interval) of the target tier
differences = []
mae_list = []
rms_list = []
r2_list = []
original_pitch_vector = []
reconstructed_pitch_victor = []
for pos, targetIntv in enumerate(tg[targetTier]):
if pos in POSdisplay:
print('Stylizing: {} contours'.format(prog.progressstring(pos)))
# get the support intervals
# which are those in supportTier that overlap with targetIntv
supportIntvs = stylize.getSupportIntvs(targetIntv,
supportTier=tg[speakerTier])
try:
tag = stylize.getTags(targetIntv, tg[tagTier])
except:
tag = None
#compute style of current interval
### Create a list of neighboring intervals ###
list_interval_neighbors = []
#pos, targetIntv in enumerate(tg[targetTier]):
if num_target_neighbors:
for i in range(1,(num_target_neighbors+1)):
if pos-i >= 0:
list_interval_neighbors.insert(0, tg[targetTier][pos-i])
for i in range(1, (num_target_neighbors+1)):
if pos+i <= len(tg[tagTier])-1:
list_interval_neighbors.append(tg[targetTier][pos+i])
### Keep only neighbors that are in intersection with the support
if limit_neighbors_to_support:
list_interval_neighbors = TextGrid.getMatchingIntervals(supportIntvs, list_interval_neighbors)
# it seems that the window will need to be calculated before this step.
out = \
stylize.stylizeObject(\
list_interval_neighbors = list_interval_neighbors, \
targetIntv = targetIntv, supportIntvs = supportIntvs,\
inputPitch = inputPitch, alpha=alpha, exclude_orphans=exclude_orphans, linear_regression=linear_regression, glissando_threshold=glissando_threshold)
if out:
(style_glo,style_loc,\
targetTimes,deltaTargetPitch, deltaTargetPitchSmooth, \
reference, reference_loc, rangeRegisterInSemitones, loccalDynamicRegister) = out
try:
avg_diff, mae, rms, r2, reconstructedPitch = evaluateStylization(style_glo, targetTimes, deltaTargetPitchSmooth)
original_pitch_vector.extend(deltaTargetPitchSmooth)
reconstructed_pitch_victor.extend(reconstructedPitch)
differences.append(avg_diff)
# mae_list.append(mae)
# rms_list.append(rms)
# r2_list.append(r2)
except:
print("problem")
else:
# when stylization fails, fill output tiers with empty content
# then skip visulization
style_glo = ""
style_loc = ""
concatenatedTag = ""
newInterval = TextGrid.Interval(targetIntv.xmin(), targetIntv.xmax(),
style_glo)
newTier.append(newInterval)
newIntervalLoc = TextGrid.Interval(targetIntv.xmin(),
targetIntv.xmax(), style_loc)
newTierLoc.append(newIntervalLoc)
if exportTag:
newIntervalTag = TextGrid.Interval(targetIntv.xmin(),
targetIntv.xmax(), concatenatedTag)
newTierTag.append(newIntervalTag)
continue
# debug
if len(style_glo) != 2 and len(style_glo) != 4 and len(style_glo) != 6:
print((
'Error: a global style code {} incorrect !'.format(style_glo)))
exit()
#continue
if len(style_loc) != 2 and len(style_loc) != 4 and len(style_loc) != 6:
print(
('Error: a local style code{} incorrect !'.format(style_loc)))
exit()
#continue
#prepare exportation of smoothed
if isinstance(deltaTargetPitchSmooth, (np.ndarray, list)):
if len(deltaTargetPitchSmooth) == len(targetTimes):
reference_semitones = stylize.hz2semitone(reference)
smooth_hz = [
stylize.semitone2hz(delta + reference_semitones)
for delta in deltaTargetPitchSmooth
]
smooth_total = np.concatenate((smooth_total, smooth_hz))
time_total = np.concatenate((time_total, targetTimes))
# give null stylization on empty segment
if targetIntv.mark():
stylesGlo += [style_glo]
stylesDynLoc += [style_loc]
else:
style_glo = '';
style_loc = '';
#then add an interval with that style to the (new) style tier
newInterval = TextGrid.Interval(targetIntv.xmin(), targetIntv.xmax(),
style_glo)
newTier.append(newInterval)
newIntervalLoc = TextGrid.Interval(targetIntv.xmin(),
targetIntv.xmax(), style_loc)
newTierLoc.append(newIntervalLoc)
if exportTag:
# concatenate tags falling in the target interval and
if targetIntv.mark():
concatenatedTag = tag
else:
# give null tag for empty target
concatenatedTag = ""
# put the concatenated result in newIntervalTag
newIntervalTag = TextGrid.Interval(targetIntv.xmin(),
targetIntv.xmax(), concatenatedTag)
newTierTag.append(newIntervalTag)
#compute figure either for examples or for export in PDF file
if support != None:
num_of_targets_in_support += 1
supportPreviousXmin = support.time[0]
support = stylize.intv2customPitchObj(supportIntvs, inputPitch)
is_new_support = (support.time[0] != supportPreviousXmin)
if exportFigures and is_new_support and haveImgInbuf:
try:
# adjust figure width to courve length for better display
#fig.set_figwidth(max(13 * len(support.time) / 100.0 * num_of_targets_in_support / 5, 13))
num_of_targets_in_support = 0
pdf.savefig(fig)
if examplesDisplayCount:
pl.show()
examplesDisplayCount -= 1
fig.clf()
haveImgInbuf = False
except:
# debug
print('Error: fail to save figures in PDF !')
#pl.show();
fig.clf()
haveImgInbuf = False
else:
supportPreviousXmin = None
support = stylize.intv2customPitchObj(supportIntvs, inputPitch)
is_new_support = True
# draw figure
try:
fig = pl.gcf()
fig = stylize.show_stylization(\
original=deltaTargetPitch,\
smooth=deltaTargetPitchSmooth,\
style1=style_glo,\
style2=style_loc,\
targetIntv=targetIntv,\
register=reference,\
register_loc=loccalDynamicRegister,\
support=support,\
time_org=targetTimes,\
figIn=fig, is_new_support=is_new_support,
rangeRegisterInSemitones = rangeRegisterInSemitones, alpha=alpha,tag = tag,\
supportName=speakerTier,targetName=targetTier,tagName=tagTier, exclude_orphans = exclude_orphans, linear_regression = linear_regression)
haveImgInbuf = True
except:
pass
if exportFigures and haveImgInbuf:
# adjust figure width to contour length for last figure
fig.set_figwidth(max(13 * len(support.time) / 100.0 * num_of_targets_in_support / 5, 13))
num_of_targets_in_support = 0
pdf.savefig(fig)
if examplesDisplayCount:
pl.show()
examplesDisplayCount -= 1
fig.clf()
haveImgInbuf = False
print('Stylizing: {} contours'.format(prog.progressstring(len(tg[targetTier]))))
#done, now writing tier into textgrid and saving textgrid
print(('Saving computed styles in file %s' % outputTextgridFile))
tg.append(newTier)
tg.append(newTierLoc)
if exportTag:
tg.append(newTierTag)
tg.write(outputTextgridFile)
#print('Exporting smoothed pitchs in Binary PitchTierfile %s' %
# outputPitchTierFile)
#praatUtil.writeBinPitchTier(outputPitchTierFile, time_total, smooth_total)
print('Exporting figures in PDF file %s' % outputFigureFile)
if exportFigures: pdf.close()
pl.close()
#print("Test 1", sum(differences)/len(differences))
#mae = mean_absolute_error(deltaTargetPitchSmooth, reconstructedPitch)
#rms = mean_squared_error(deltaTargetPitchSmooth, reconstructedPitch, squared=False)
#r2 = r2_score(deltaTargetPitchSmooth, reconstructedPitch)
mae = mean_absolute_error(original_pitch_vector, reconstructed_pitch_victor)
rms = mean_squared_error(original_pitch_vector, reconstructed_pitch_victor, squared=False)
r2 = r2_score(original_pitch_vector, reconstructed_pitch_victor)
#print("MAE:", mae)
#print("RMS:", rms)
#print("R2:", r2)
#print("\n")
mae_all.append(mae)
rms_all.append(rms)
r2_all.append(r2)
print('List of files remained to be analyzed: '.format(len(tgFiles)))
for i, name in enumerate(tgFiles):
print('{}: {}'.format(i, name))
# Export files to TSV
if exportTSV:
print(
"""
------------------------------------------------------------------
Converting output TextGrids to .tsv:
------------------------------------------------------------------
""")
global_tier = targetTier+"StyleGlo"
local_tier = targetTier+"StyleLoc"
if exportTag:
tierslist = [targetTier, tagTier, global_tier, local_tier]
else:
tierslist = [targetTier, global_tier, local_tier]
number_total_files = len(outputTextgridFiles_all)
number_good_files = 0
number_bad_files = 0
for file in outputTextgridFiles_all:
print("Converting %s to .tsv..." % file, end=" ")
try:
textgrid2tsv.build_tsv(file, tierslist)
print("- success")
number_good_files += 1
except Exception as e:
print("- FAILURE")
print("- Conversion failed with following error message. Check input for likely issues.")
print("- "+str(e))
print("\n")
number_bad_files+=1
continue
print("\nOf {} files, {} were successfully converted to .tsv, and {} failed\n\n".format(number_total_files, number_good_files, number_bad_files))
#Now output statistics
#---------------------
labs = ['Stylization over Global Register',\
'Stylization over Local Register']
for i, styles in enumerate([stylesGlo, stylesDynLoc]):
print(('Type of Stylization: {}'.format(labs[i])))
count = {}
for unique_style in set(styles):
if not len(unique_style): continue
count[unique_style] = styles.count(unique_style)
#valeurs triees par importance decroissante
unsorted_values = np.array(list(count.values()))
nbStylesRaw = len(unsorted_values)
total = float(sum(unsorted_values))
#remove styles that appear less than 0.5 percents of the time
"""
for style in list(count.keys()):
if count[style] / total < 0.005: del count[style]
"""
unsorted_values = np.array(list(count.values()))
stylesNames = list(count.keys())
argsort = np.argsort(unsorted_values)[::-1] # from most to less important
sorted_values = unsorted_values[argsort]
total = float(sum(unsorted_values))
L = min(len(list(count.keys())), 20)
print("""
------------------------------------------------------------------
SLAM analysis overall summary:
------------------------------------------------------------------
- %d intervals to stylize.
- %d intervals with a non empty contour (others are unvoiced)
- %d distinctive forms of contour appearing in total
------------------------------------------------------------------
- The form of contour along with their frequency are:"""%(
totalN,\
len(styles),\
len(set(styles))))
styleNames = sorted(count, key=count.get)
styleNames.reverse()
for styleName in styleNames:
print(
('\t%s\t:\t:%0.1f%% (%d occurrences)' %
(styleName, count[styleName] / total * 100.0, count[styleName])))
print('''
x------------------------------------------x-----------------------------x
| explained proportion of the observations | number of forms of contours |
| (percents) | |
x------------------------------------------x-----------------------------x''')
cumulative_values = np.cumsum(sorted_values)
cumulative_values = cumulative_values / float(cumulative_values[-1])
for P in [70, 75, 80, 85, 90, 95, 99]:
N = np.nonzero(cumulative_values > float(P) / 100.0)[0][0] + 1
print(
'| %2.0f | %2.0f |'
% (P, N))
print('x------------------------------------------x---------------------x')
print('SLAM: done in {}'.format(stylize.get_duration(t1, time.time())))
#print("All MAE:", mae_all)
#print(sum(mae_all)/len(mae_all))
#print("All RMS", rms_all)
#print(sum(rms_all)/len(rms_all))
#print("All R2", r2_all)
#print(sum(r2_all)/len(r2_all))