def get_roc_curves(phn, num_mix_params,
                          train_example_lengths,bgd,
                          train_path,file_indices,
                          sp,ep):
    FOMS = collections.defaultdict(list)
    for num_mix in num_mix_params:
        if num_mix > 1:
            outfile = np.load('data/%d_templates.npz' % num_mix)
            templates = tuple( outfile['arr_%d'%i] for i in xrange(len(outfile.files)))
        else:
            templates = (np.load('data/1_templates.npy')[0],)
        detection_array = np.zeros((train_example_lengths.shape[0],
                            train_example_lengths.max() + 2),dtype=np.float32)
        linear_filters_cs = et.construct_linear_filters(templates,
                                                        bgd)
        np.savez('data/linear_filter_%d.npz'% num_mix,*(tuple(lfc[0] for lfc in linear_filters_cs)))
        np.savez('data/c_%d.npz'%num_mix,*(tuple(lfc[1] for lfc in linear_filters_cs)))
        syllable = np.array((phn,))
        (detection_array,
         example_start_end_times,
         detection_lengths,
         detection_template_ids)=gtrd.get_detection_scores_mixture_named_params(
             train_path,
             file_indices,
             detection_array,
             syllable,
             linear_filters_cs,S_config=sp,
             E_config=ep,
             verbose = True,
             num_examples =-1,
             return_detection_template_ids=True)

        np.save('data/detection_array_%d.npy' % num_mix,detection_array)
        np.save('data/detection_template_ids_%d.npy' % num_mix,detection_template_ids)
        np.save('data/detection_lengths_%d.npy' % num_mix,detection_lengths)
        if num_mix == num_mix_params[0]:
            out = open('data/example_start_end_times.pkl','wb')
            cPickle.dump(example_start_end_times,out)
            out.close()
        window_start = -int(np.mean(tuple( t.shape[0] for t in templates))/3.+.5)
        window_end = -window_start
        max_detect_vals = rf.get_max_detection_in_syllable_windows(detection_array,
                                                                   example_start_end_times,
                                                                   detection_lengths,
                                                                   window_start,
                                                                   window_end)
        max_detect_vals = max_detect_vals[:1000]
        np.save('data/max_detect_vals_%d_%s.npy' % (num_mix,phn),max_detect_vals)
        C0 = int(np.mean(tuple( t.shape[0] for t in templates))/3.+.5)
        C1 = int( 33 * 1.5 + .5)
        frame_rate = 1/.005
        fpr, tpr = rf.get_roc_curve(max_detect_vals,
                                    detection_array,
                                    np.array(detection_lengths),
                                    example_start_end_times,
                                    C0,C1,frame_rate)
        np.save('data/fpr_%d_%s.npy' % (num_mix,phn),
                fpr)
        np.save('data/tpr_%d_%s.npy' % (num_mix,phn),
                tpr)
bgd  = np.clip(np.load("data/aar_bgd_mel.npy"),.1,.4)
test_example_lengths =np.load("data/test_example_lengths.npy")
test_path = "/home/mark/Template-Speech-Recognition/Data/Test/"
test_file_indices =np.load("data/test_file_indices.npy")

for num_mix in num_mix_params:
    templates = tuple(np.clip(T,.01,.99) for T in (np.load('aar1_templates_%d.npz' % num_mix))['arr_0'])
    detection_array = np.zeros((test_example_lengths.shape[0],
                            test_example_lengths.max() + 2),dtype=np.float32)
    linear_filters_cs = et.construct_linear_filters(templates,
                                                    bgd)
    syllable = np.array(['aa','r'])
    detection_array,example_start_end_times, detection_lengths = gtrd.get_detection_scores_mixture_named_params(test_path,test_file_indices,
                                                                                                   detection_array,
                                                                                                   syllable,
                                                                                                   linear_filters_cs,S_config=sp,
                                              E_config=ep,
                         verbose = False,
                         num_examples =-1)
    np.save('data/detection_array_aar_%d.npy' % num_mix,detection_array)
    if num_mix == 2:
        out = open('data/example_start_end_times_aar.pkl','wb')
        cPickle.dump(example_start_end_times,out)
        out.close()
        out = open('data/detection_lengths_aar.pkl','wb')
        cPickle.dump(detection_lengths,out)
        out.close()
    window_start = -10
    window_end = 10
    max_detect_vals = rf.get_max_detection_in_syllable_windows(detection_array,
                                                        example_start_end_times,
     templates = (np.load('data/1_templates.npy')[0],)
 detection_array = np.zeros((train_example_lengths.shape[0],
                     train_example_lengths.max() + 2),dtype=np.float32)
 linear_filters_cs = et.construct_linear_filters(templates,
                                                 bgd)
 np.savez('data/linear_filter_%d.npz'% num_mix,*(tuple(lfc[0] for lfc in linear_filters_cs)))
 np.savez('data/c_%d.npz'%num_mix,*(tuple(lfc[1] for lfc in linear_filters_cs)))
 syllable = np.array((phn,))
 (detection_array,
  example_start_end_times,
  detection_lengths,
  detection_template_ids)=gtrd.get_detection_scores_mixture_named_params(
      train_path,
      file_indices,
      detection_array,
      syllable,
      linear_filters_cs,S_config=sp,
      E_config=ep,
      verbose = True,
      num_examples =-1,
      return_detection_template_ids=True)
 np.save('data/detection_array_%d.npy' % num_mix,detection_array)
 np.save('data/detection_template_ids_%d.npy' % num_mix,detection_template_ids)
 np.save('data/detection_lengths_%d.npy' % num_mix,detection_lengths)
 if num_mix == num_mix_params[0]:
     out = open('data/example_start_end_times.pkl','wb')
     cPickle.dump(example_start_end_times,out)
     out.close()
 window_start = -int(np.mean(tuple( t.shape[0] for t in templates))/3.+.5)
 window_end = -window_start
 max_detect_vals = rf.get_max_detection_in_syllable_windows(detection_array,
                                                            example_start_end_times,