# Path: tde/data/sets.py
# def Pclus(clsdict):
#     r"""Generate Pclus - all the pairs of FragmentTokens per cluster
# 
#     .. math::
# 
#        P_{\mathrm{clus}} = \{((i, j), (k, l)) | \exists c \in C_{\mathrm{disc}} \land (i,j)\in c \land (j,k) \in c\}
# 
#     where :math:`C_{\mathrm{disc}}` is the set of discovered clusters, each set being a set of fragments.
# 
#     Parameters
#     ----------
#     clsdict : ClassDict
# 
#     Returns
#     -------
#     Iterator (FragmentToken, FragmentToken) pairs
# 
#     """
#     return clsdict.iter_pairs(within=True, order=True)
# 
# def Psubs(clsdict, corpus, minlength=3, maxlength=20):
#     """
#     Generate Psubs - the substring completion of a set of pairs.
# 
#     Psubs is the association between all substrings of the pairs in classdict.
# 
#     Parameters
#     ----------
#     clsdict : ClassDict
#     corpus : Corpus
#     minlength : int, optional
#         minimum number of phones for the substrings
#     maxlength : int, optional
#         maximum number of phones for the substrings
# 
#     Returns
#     -------
#     Iterator over (FragmentToken, FragmentToken) pairs
# 
#     """
#     sub_pairs = (pairwise_substring_completion(f1, f2, corpus,
#                                                minlength,
#                                                maxlength)
#                  for f1, f2 in clsdict.iter_pairs(within=True, order=True))
#     return unique(flatten(sub_pairs))
# 
# def nmatch(pairs):
#     """Count the number of pairs of fragments per annotation string.
# 
#     .. math::
#        \mathrm{nmatch}(t, P) = |\{(x, (i, j))\in P | T_{i,j} = t\}|
# 
#     Parameters
#     ----------
#     pairs : iterable over (FragmentToken, FragmentToken) pairs
#     corpus : Corpus
# 
#     Returns
#     -------
#     d : dict from string tuple to int
#         Annotation counts.
# 
#     """
#     return Counter(f.mark
#                    for _, f in pairs)
# 
# def typeset(pairs):
#     """
#     Yield the unique marks in a pair iterator.
# 
#     Parameters
#     ----------
#     pairs : Iterator over (FragmentToken, FragmentToken) pairs
# 
#     Returns
#     -------
#     Iterator over strings
#         Unique marks.
# 
#     """
#     return unique(f.mark for f in flatten(pairs))
# 
# def weights(pairs):
#     """
#     Calculate the relative frequencies of the marks in a pair iterator.
# 
#     Parameters
#     ----------
#     pairs : Iterator over (FragmentToken, FragmentToken) pairs
# 
#     Returns
#     -------
#     dict from string to float
#         Relative frequencies of the marks.
#     """
#     total = iterator_length(unique_flatten(pairs))
#     fs = freqs(pairs)
#     return {t: fs[t] / total for t in fs}
# 
# Path: tde/util/printing.py
# def banner(s):
#     l = len(s)
#     return '-'*l+'\n'+s+'\n'+'-'*l
# 
# @contextmanager
# def verb_print(label, verbose=False, when_done=False,
#                timeit=False, with_dots=False):
#     if timeit:
#         t0 = time.time()
#     if verbose:
#         msg = label + ('...' if with_dots else '') + ('' if when_done else '\n')
#         print msg,
#         sys.stdout.flush()
#     try:
#         yield
#     finally:
#         if verbose and when_done:
#             if timeit:
#                 print 'done. Time: {0:.3f}s'.format(time.time() - t0)
#             else:
#                 print 'done.'
#             sys.stdout.flush()
#         elif verbose and timeit:
#             print '{1}: time: {0:.3f}s'.format(time.time() - t0, label)
#             sys.stdout.flush()
# 
# def pretty_pairs(pclus_set):
#     strings = [('<{0} [{1:.3f},{2:.3f}] ({3})>'.format(f1.name, f1.interval.start,
#                                                        f1.interval.end, ' '.join(f1.mark)),
#                 '<{0} [{1:.3f},{2:.3f}] ({3})>'.format(f2.name, f2.interval.start,
#                                                        f2.interval.end, ' '.join(f2.mark)))
#                for f1, f2 in pclus_set]
#     if len(strings) == 0:
#         return ''
#     longest = max(len(x[0]) for x in strings)
#     return '\n'.join('{1:{0}s} - {2:{0}s}'.format(longest, s1, s2)
#                      for s1, s2 in strings)
# 
# Path: tde/util/functions.py
# def intersection(iterable1, iterable2):
#     set1 = set(iterable1)
#     seen = set()
#     seen_add = seen.add
#     for element in iterable2:
#         if element in seen:
#             continue
#         seen_add(element)
#         if element in set1:
#             yield element


from pprint import pformat
from tde.data.sets import Pclus, Psubs, nmatch, typeset, weights
from tde.util.printing import banner, verb_print, pretty_pairs
from tde.util.functions import intersection
import numpy as np
"""

from __future__ import division





def make_pgold(gold_clsdict, verbose, debug):
    with verb_print('constructing pgold set', verbose, True, True):
        pgold = list(Pclus(gold_clsdict))
    if debug:
        print banner('PGOLD ({0})'.format(len(pgold)))
        print pretty_pairs(pgold)
        print
    return pgold


def make_pdisc(disc_clsdict, verbose, debug):
    with verb_print('constructing pdisc set', verbose, True, True):
        pdisc = list(Pclus(disc_clsdict))
    if debug:
        print banner('PDISC ({0})'.format(len(pdisc)))
        print pretty_pairs(pdisc)
        print
    return pdisc


def make_psubs(disc_clsdict, corpus, minlength, maxlength,
               verbose, debug):

# Path: tde/data/sets.py
# def Pclus(clsdict):
#     r"""Generate Pclus - all the pairs of FragmentTokens per cluster
# 
#     .. math::
# 
#        P_{\mathrm{clus}} = \{((i, j), (k, l)) | \exists c \in C_{\mathrm{disc}} \land (i,j)\in c \land (j,k) \in c\}
# 
#     where :math:`C_{\mathrm{disc}}` is the set of discovered clusters, each set being a set of fragments.
# 
#     Parameters
#     ----------
#     clsdict : ClassDict
# 
#     Returns
#     -------
#     Iterator (FragmentToken, FragmentToken) pairs
# 
#     """
#     return clsdict.iter_pairs(within=True, order=True)
# 
# def Psubs(clsdict, corpus, minlength=3, maxlength=20):
#     """
#     Generate Psubs - the substring completion of a set of pairs.
# 
#     Psubs is the association between all substrings of the pairs in classdict.
# 
#     Parameters
#     ----------
#     clsdict : ClassDict
#     corpus : Corpus
#     minlength : int, optional
#         minimum number of phones for the substrings
#     maxlength : int, optional
#         maximum number of phones for the substrings
# 
#     Returns
#     -------
#     Iterator over (FragmentToken, FragmentToken) pairs
# 
#     """
#     sub_pairs = (pairwise_substring_completion(f1, f2, corpus,
#                                                minlength,
#                                                maxlength)
#                  for f1, f2 in clsdict.iter_pairs(within=True, order=True))
#     return unique(flatten(sub_pairs))
# 
# def nmatch(pairs):
#     """Count the number of pairs of fragments per annotation string.
# 
#     .. math::
#        \mathrm{nmatch}(t, P) = |\{(x, (i, j))\in P | T_{i,j} = t\}|
# 
#     Parameters
#     ----------
#     pairs : iterable over (FragmentToken, FragmentToken) pairs
#     corpus : Corpus
# 
#     Returns
#     -------
#     d : dict from string tuple to int
#         Annotation counts.
# 
#     """
#     return Counter(f.mark
#                    for _, f in pairs)
# 
# def typeset(pairs):
#     """
#     Yield the unique marks in a pair iterator.
# 
#     Parameters
#     ----------
#     pairs : Iterator over (FragmentToken, FragmentToken) pairs
# 
#     Returns
#     -------
#     Iterator over strings
#         Unique marks.
# 
#     """
#     return unique(f.mark for f in flatten(pairs))
# 
# def weights(pairs):
#     """
#     Calculate the relative frequencies of the marks in a pair iterator.
# 
#     Parameters
#     ----------
#     pairs : Iterator over (FragmentToken, FragmentToken) pairs
# 
#     Returns
#     -------
#     dict from string to float
#         Relative frequencies of the marks.
#     """
#     total = iterator_length(unique_flatten(pairs))
#     fs = freqs(pairs)
#     return {t: fs[t] / total for t in fs}
# 
# Path: tde/util/printing.py
# def banner(s):
#     l = len(s)
#     return '-'*l+'\n'+s+'\n'+'-'*l
# 
# @contextmanager
# def verb_print(label, verbose=False, when_done=False,
#                timeit=False, with_dots=False):
#     if timeit:
#         t0 = time.time()
#     if verbose:
#         msg = label + ('...' if with_dots else '') + ('' if when_done else '\n')
#         print msg,
#         sys.stdout.flush()
#     try:
#         yield
#     finally:
#         if verbose and when_done:
#             if timeit:
#                 print 'done. Time: {0:.3f}s'.format(time.time() - t0)
#             else:
#                 print 'done.'
#             sys.stdout.flush()
#         elif verbose and timeit:
#             print '{1}: time: {0:.3f}s'.format(time.time() - t0, label)
#             sys.stdout.flush()
# 
# def pretty_pairs(pclus_set):
#     strings = [('<{0} [{1:.3f},{2:.3f}] ({3})>'.format(f1.name, f1.interval.start,
#                                                        f1.interval.end, ' '.join(f1.mark)),
#                 '<{0} [{1:.3f},{2:.3f}] ({3})>'.format(f2.name, f2.interval.start,
#                                                        f2.interval.end, ' '.join(f2.mark)))
#                for f1, f2 in pclus_set]
#     if len(strings) == 0:
#         return ''
#     longest = max(len(x[0]) for x in strings)
#     return '\n'.join('{1:{0}s} - {2:{0}s}'.format(longest, s1, s2)
#                      for s1, s2 in strings)
# 
# Path: tde/util/functions.py
# def intersection(iterable1, iterable2):
#     set1 = set(iterable1)
#     seen = set()
#     seen_add = seen.add
#     for element in iterable2:
#         if element in seen:
#             continue
#         seen_add(element)
#         if element in set1:
#             yield element


# Path: tde/measures/match.py
from pprint import pformat
from tde.data.sets import Pclus, Psubs, nmatch, typeset, weights
from tde.util.printing import banner, verb_print, pretty_pairs
from tde.util.functions import intersection
import numpy as np

"""

from __future__ import division





def make_pgold(gold_clsdict, verbose, debug):
    with verb_print('constructing pgold set', verbose, True, True):
        pgold = list(Pclus(gold_clsdict))
    if debug:
        print banner('PGOLD ({0})'.format(len(pgold)))
        print pretty_pairs(pgold)
        print
    return pgold


def make_pdisc(disc_clsdict, verbose, debug):
    with verb_print('constructing pdisc set', verbose, True, True):
        pdisc = list(Pclus(disc_clsdict))
    if debug:
        print banner('PDISC ({0})'.format(len(pdisc)))
        print pretty_pairs(pdisc)
        print
    return pdisc


def make_psubs(disc_clsdict, corpus, minlength, maxlength,
               verbose, debug):

    with verb_print('constructing psubs set', verbose, True, True):