root_path = '/var/tmp/stoehr/Template-Speech-Recognition/'
data_path = root_path + 'Data/'
exp_path = root_path + 'Notebook/1/'
tmp_data_path = exp_path + 'data/'
import sys, os, cPickle
sys.path.append(root_path)

import template_speech_rec.get_train_data as gtrd
import template_speech_rec.estimate_template as et
train_data_path = root_path+'Data/Train/'

file_indices = gtrd.get_data_files_indices(train_data_path)
syllable = np.array(['aa','r'])
avg_bgd, syllable_examples, backgrounds = gtrd.get_syllable_examples_backgrounds_files(train_data_path,
                                                                                       file_indices,
                                                                                       syllable,
                                                                                       
                                                                                       num_examples=-1,
                                                                                       verbose=True)


clipped_bgd = np.clip(avg_bgd.E,.1,.4)
np.save(tmp_data_path+'clipped_bgd_102012.npy',clipped_bgd)

padded_examples, lengths = et.extend_examples_to_max(clipped_bgd,syllable_examples,
                           return_lengths=True)

aar_template, aar_registered = et.register_templates_time_zero(syllable_examples,min_prob=.01)

test_example_lengths = gtrd.get_detect_lengths(data_path+'Test/')

np.save(tmp_data_path+'test_example_lengths_102012.npy',test_example_lengths)
num_parts = 100

old_devel_data_path = root_path + 'Development/092412/data/'
parts = np.load(old_devel_data_path+'bm_templates%d_%d.npy' % (lower_cutoff,num_parts))


log_part_blocks, log_invpart_blocks = gtrd.reorg_parts_for_fast_filtering(parts)
log_part_blocks = log_part_blocks.astype(np.float32)
log_invpart_blocks = log_invpart_blocks.astype(np.float32)


file_indices = gtrd.get_data_files_indices(train_data_path)
syllable = np.array(['b','ay'])
avg_bgd, syllable_examples, backgrounds = gtrd.get_syllable_examples_backgrounds_files(train_data_path,
                                            file_indices,
                                                                                       syllable,

                                            num_examples=-1,
                                            verbose=True)

parts_avg_bgd, parts_syllable_examples, parts_backgrounds = gtrd.get_syllable_examples_backgrounds_files(train_data_path,
                                            file_indices,
                                                                                       syllable,
                                            log_part_blocks,
                                            log_invpart_blocks,

                                            num_examples=-1,
                                            verbose=True)