# 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
#!/usr/bin/env python



C_vals = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100, 1000]
C_strs = ['1e-05', '0.0001', '0.001', '0.01', '1.0', '10.0', '100.0', '1000.0']
feature_types = ['base', 'mul', 'mul_s', 'mul_quad', 'landmark']
MODEL_TYPE='bellman'

def estimate_performance(results_file):
    if type(results_file) is str:
        results_file = h5py.File(results_file, 'r')

    num_knots = 0
    knot_inds = []
    not_inds = []
    ctr = 0
    n_checks = len(results_file) - 1
    for (i_task, task_info) in results_file.iteritems():
        sys.stdout.write("\rchecking task {} / {}        ".format(ctr, n_checks))
        sys.stdout.flush()
        ctr += 1
        if str(i_task) == 'args':
            continue
        # if int(i_task) > 3:
        #     break
        N_steps = len(task_info)
        final_cld = task_info[str(N_steps-1)]['next_state']['rope_nodes'][:]

# 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

#!/usr/bin/env python



C_vals = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100, 1000]
C_strs = ['1e-05', '0.0001', '0.001', '0.01', '1.0', '10.0', '100.0', '1000.0']
feature_types = ['base', 'mul', 'mul_s', 'mul_quad', 'landmark']
MODEL_TYPE='bellman'

def estimate_performance(results_file):
    if type(results_file) is str:
        results_file = h5py.File(results_file, 'r')

    num_knots = 0
    knot_inds = []
    not_inds = []
    ctr = 0
    n_checks = len(results_file) - 1
    for (i_task, task_info) in results_file.iteritems():
        sys.stdout.write("\rchecking task {} / {}        ".format(ctr, n_checks))
        sys.stdout.flush()
        ctr += 1
        if str(i_task) == 'args':
            continue
        # 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):