# Path: lfd/rapprentice/knot_classifier.py
# def isKnot(rope_nodes):
#     (crossings, crossings_links_inds, cross_pairs, rope_closed) = calculateCrossings(rope_nodes)
#     # simplify crossings a bit
#     crossings, crossings_links_inds, cross_pairs = remove_consecutive_crossings(crossings, crossings_links_inds, cross_pairs)
#     crossings, crossings_links_inds, cross_pairs = remove_consecutive_cross_pairs(crossings, crossings_links_inds, cross_pairs)
#     s = crossingsToString(crossings)
#     
#     # special cases
#     if cross_pairs == set([(2, 7), (5, 10), (3, 6), (1, 8), (4, 9)]):
#         return True
#     if cross_pairs == set([(3, 8), (2, 5), (1, 6), (4, 7)]):
#         return True
#     
#     knot_topologies = ['uououo', 'uoouuoou']
#     for top in knot_topologies:
#         flipped_top = top.replace('u','t').replace('o','u').replace('t','o')
#         if top in s and crossings_match(cross_pairs, top, s):
#             return True
#         if top[::-1] in s and crossings_match(cross_pairs, top[::-1], s):
#             return True
#         if flipped_top in s and crossings_match(cross_pairs, flipped_top, s):
#             return True
#         if flipped_top[::-1] in s and crossings_match(cross_pairs, flipped_top[::-1], s):
#             return True
# 
#     if rope_closed:
#         return False  # There is no chance of it being a knot with one end
#                       # of the rope crossing the knot accidentally
# 
#     knot_topology_variations = ['ououuouo', 'ouoououu']
#     for top in knot_topology_variations:
#         flipped_top = top.replace('u','t').replace('o','u').replace('t','o')
#         if top in s and crossings_var_match(cross_pairs, top, s):
#             return True
#         if top[::-1] in s and crossings_var_match(cross_pairs, top[::-1], s):
#             return True
#         if flipped_top in s and crossings_var_match(cross_pairs, flipped_top, s):
#             return True
#         if flipped_top[::-1] in s and crossings_match(cross_pairs, flipped_top[::-1], s):
#             return True
# 
#     return False


import argparse
import h5py
import sys
import os.path as osp
from lfd.rapprentice.knot_classifier import isKnot
from string import lower
        # if int(i_task) > 3:
        #     break
        N_steps = len(task_info)
        final_cld = task_info[str(N_steps-1)]['next_state']['rope_nodes'][:]
        if isKnot(final_cld):
            knot_inds.append(int(i_task))
            num_knots += 1
        else:
            not_inds.append(int(i_task))

    print 
    
    return num_knots, knot_inds, not_inds

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("outfile")
    parser.add_argument("--baseline", type=str, default='../data/evals/7_3_0.1_baseline.h5')
    args = parser.parse_args()
    recompute_results = True
    if osp.exists(args.outfile):
        user_resp = raw_input("Overwrite results file {}[y/N]".format(args.outfile))
        recompute_results = lower(user_resp) == 'y'
    if recompute_results:
        outf = h5py.File(args.outfile, 'w')
        base_successes, a, b = estimate_performance(args.baseline)
        base_rate = base_successes / float(len(a) + len(b))
        outf['base_rate'] = base_rate
        for f in feature_types:
            for c in C_strs:

# Path: lfd/rapprentice/knot_classifier.py
# def isKnot(rope_nodes):
#     (crossings, crossings_links_inds, cross_pairs, rope_closed) = calculateCrossings(rope_nodes)
#     # simplify crossings a bit
#     crossings, crossings_links_inds, cross_pairs = remove_consecutive_crossings(crossings, crossings_links_inds, cross_pairs)
#     crossings, crossings_links_inds, cross_pairs = remove_consecutive_cross_pairs(crossings, crossings_links_inds, cross_pairs)
#     s = crossingsToString(crossings)
#     
#     # special cases
#     if cross_pairs == set([(2, 7), (5, 10), (3, 6), (1, 8), (4, 9)]):
#         return True
#     if cross_pairs == set([(3, 8), (2, 5), (1, 6), (4, 7)]):
#         return True
#     
#     knot_topologies = ['uououo', 'uoouuoou']
#     for top in knot_topologies:
#         flipped_top = top.replace('u','t').replace('o','u').replace('t','o')
#         if top in s and crossings_match(cross_pairs, top, s):
#             return True
#         if top[::-1] in s and crossings_match(cross_pairs, top[::-1], s):
#             return True
#         if flipped_top in s and crossings_match(cross_pairs, flipped_top, s):
#             return True
#         if flipped_top[::-1] in s and crossings_match(cross_pairs, flipped_top[::-1], s):
#             return True
# 
#     if rope_closed:
#         return False  # There is no chance of it being a knot with one end
#                       # of the rope crossing the knot accidentally
# 
#     knot_topology_variations = ['ououuouo', 'ouoououu']
#     for top in knot_topology_variations:
#         flipped_top = top.replace('u','t').replace('o','u').replace('t','o')
#         if top in s and crossings_var_match(cross_pairs, top, s):
#             return True
#         if top[::-1] in s and crossings_var_match(cross_pairs, top[::-1], s):
#             return True
#         if flipped_top in s and crossings_var_match(cross_pairs, flipped_top, s):
#             return True
#         if flipped_top[::-1] in s and crossings_match(cross_pairs, flipped_top[::-1], s):
#             return True
# 
#     return False


# Path: lfd/mmqe/auto_performance.py
import argparse
import h5py
import sys
import os.path as osp
from lfd.rapprentice.knot_classifier import isKnot
from string import lower

        # if int(i_task) > 3:
        #     break
        N_steps = len(task_info)
        final_cld = task_info[str(N_steps-1)]['next_state']['rope_nodes'][:]
        if isKnot(final_cld):
            knot_inds.append(int(i_task))
            num_knots += 1
        else:
            not_inds.append(int(i_task))

    print 
    
    return num_knots, knot_inds, not_inds

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("outfile")
    parser.add_argument("--baseline", type=str, default='../data/evals/7_3_0.1_baseline.h5')
    args = parser.parse_args()
    recompute_results = True
    if osp.exists(args.outfile):
        user_resp = raw_input("Overwrite results file {}[y/N]".format(args.outfile))
        recompute_results = lower(user_resp) == 'y'
    if recompute_results:
        outf = h5py.File(args.outfile, 'w')
        base_successes, a, b = estimate_performance(args.baseline)
        base_rate = base_successes / float(len(a) + len(b))
        outf['base_rate'] = base_rate
        for f in feature_types:
            for c in C_strs:

                results_fname = '../data/evals/jul_6_{}_0.1_c={}_{}.h5'.format(f, c, MODEL_TYPE)