def get_file_indices(file_indices_path, data_path):
    try:
        file_indices = np.load(file_indices_path)
    except:
        file_indices = gtrd.get_data_files_indices(data_path)
        np.save(file_indices_path, file_indices)
    return file_indices
import numpy as np

root_path = '/var/tmp/stoehr/Template-Speech-Recognition/'
data_path = root_path + 'Data/'
old_exp_path = root_path + 'Notebook/1/'
old_data_path = old_exp_path + 'data/'
exp_path = root_path + 'Notebook/2/'
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'])

clipped_bgd = np.load(old_data_path+'clipped_bgd_102012.npy')

padded_examples = np.load(old_data_path+'aar_padded_examples_bgd.npy')

lengths = np.load(old_data_path+'aar_lengths.npy')


test_example_lengths = np.load(old_data_path+'test_example_lengths_102012.npy')


detection_array = np.zeros((test_example_lengths.shape[0],
                            test_example_lengths.max() + 2),dtype=np.float32)
sp = gtrd.SpectrogramParameters(
    sample_rate=16000,
    num_window_samples=320,
    num_window_step_samples=80,
    fft_length=512,
    kernel_length=7,
    freq_cutoff=3000,
    use_mel=True)

ep = gtrd.EdgemapParameters(block_length=40,
                            spread_length=1,
                            threshold=.7)

utterances_path = '/home/mark/Template-Speech-Recognition/Data/Train/'
file_indices = gtrd.get_data_files_indices(utterances_path)

syllable=('aa','r')
syllable_features,avg_bgd=gtrd.get_syllable_features_directory(utterances_path,file_indices,syllable,
                                    S_config=sp,E_config=ep,offset=0,
                                    E_verbose=False,return_avg_bgd=True)

np.save('data/aar_bgd_mel.npy',avg_bgd.E)

example_mat = gtrd.recover_example_map(syllable_features)
lengths,waveforms  = gtrd.recover_waveforms(syllable_features,example_mat)

np.savez('data/aar_waveforms_lengths.npz',waveforms,lengths,example_mat)

Slengths,Ss  = gtrd.recover_specs(syllable_features,example_mat)
root_path = '/home/mark/Template-Speech-Recognition/'
#root_path = '/var/tmp/stoehr/Template-Speech-Recognition/'
data_path = root_path + 'Data/'
old_exp_path = root_path + 'Notebook/1/'
old_data_path = old_exp_path + 'data/'
exp_path = root_path + 'Notebook/2/'
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



file_indices = gtrd.get_data_files_indices(data_path+'Test/')
num_mix = 3
i = 0

false_positive_rates =np.load(tmp_data_path+'fpr_aar_registered_new%d_%d.npy' % (num_mix,i))
true_positive_rates = np.load(tmp_data_path+'tpr_aar_registered_new%d_%d.npy' % (num_mix,i))

import matplotlib.pyplot as plt

colors = ['r','b','g','y','k','m','c']
markers = ['s','o','^','>','v','<','d','p']


plt.close()
plt.figure()
def get_params(
    args,
    sample_rate=16000,
    num_window_samples=320,
    num_window_step_samples=80,
    fft_length=512,
    kernel_length=7,
    freq_cutoff=3000,
    use_mel=False,
):
    # we get the basic file paths right here
    # TODO: make this system adaptive
    root_path = "/home/mark/Template-Speech-Recognition/"
    utterances_path = "/home/mark/Template-Speech-Recognition/Data/Train/"
    try:
        file_indices = np.load("data_parts/train_file_indices.npy")
    except:
        file_indices = gtrd.get_data_files_indices(utterances_path)
        np.save("data_parts/train_file_indices.npy", file_indices)

    num_mix_params = [1, 2, 3, 5, 7, 9]

    test_path = "/home/mark/Template-Speech-Recognition/Data/Test/"
    train_path = "/home/mark/Template-Speech-Recognition/Data/Train/"

    try:
        test_example_lengths = np.load("data_parts/test_example_lengths.npy")
        test_file_indices = np.load("data_parts/test_file_indices.npy")
    except:
        test_file_indices = gtrd.get_data_files_indices(test_path)
        test_example_lengths = gtrd.get_detect_lengths(test_file_indices, test_path)
        np.save("data_parts/test_example_lengths.npy", test_example_lengths)
        np.save("data_parts/test_file_indices.npy", test_file_indices)

    try:
        train_example_lengths = np.load("data_parts/train_example_lengths.npy")
        train_file_indices = np.load("data_parts/train_file_indices.npy")
    except:
        train_file_indices = gtrd.get_data_files_indices(train_path)
        train_example_lengths = gtrd.get_detect_lengths(train_file_indices, train_path)
        np.save("data_parts/train_example_lengths.npy", train_example_lengths)
        np.save("data_parts/train_file_indices.npy", train_file_indices)

    return (
        gtrd.SpectrogramParameters(
            sample_rate=16000,
            num_window_samples=320,
            num_window_step_samples=80,
            fft_length=512,
            kernel_length=7,
            freq_cutoff=3000,
            use_mel=args.use_mel,
        ),
        gtrd.makeEdgemapParameters(
            block_length=args.edgeMapBlockLength,
            spread_length=args.edgeMapSpreadLength,
            threshold=args.edgeMapThreshold,
        ),
        root_path,
        utterances_path,
        file_indices,
        num_mix_params,
        test_path,
        train_path,
        train_example_lengths,
        train_file_indices,
        test_example_lengths,
        test_file_indices,
    )
sp = gtrd.SpectrogramParameters(
    sample_rate=16000,
    num_window_samples=320,
    num_window_step_samples=80,
    fft_length=512,
    kernel_length=7,
    freq_cutoff=3000,
    use_mel=False)

ep = gtrd.EdgemapParameters(block_length=40,
                            spread_length=1,
                            threshold=.7)

utterances_path = '/home/mark/Template-Speech-Recognition/Data/Train/'
file_indices = gtrd.get_data_files_indices(utterances_path)

syllable=('aa','r')
syllable_features,avg_bgd=gtrd.get_syllable_features_directory(utterances_path,file_indices,syllable,
                                    S_config=sp,E_config=ep,offset=0,
                                    E_verbose=False,return_avg_bgd=True,
                                                               waveform_offset=15)

np.save('data/aar_bgd.npy',avg_bgd.E)

example_mat = gtrd.recover_example_map(syllable_features)
lengths,waveforms  = gtrd.recover_waveforms(syllable_features,example_mat)

np.savez('data/aar_waveforms_lengths.npz',waveforms,lengths,example_mat)

import scipy.io