"""
.. module:: PrefixpanRulesProcess

PrefixpanRulesProcess
*************

:Description: PrefixpanRulesProcess

    Process the files generated by prefixpan and writes a report

:Authors: bejar
    

:Version: 

:Created on: 22/06/2016 14:11 

"""

import pylab as P
from pylab import *
from pyx import *
from pyx.color import cmyk, rgb
from util.plots import plotMatrices, plotMatrix
from util.misc import normalize_matrix, compute_frequency_remap
import h5py
import numpy as np
from Config.experiments import experiments
from sklearn.metrics import pairwise_distances_argmin_min
import seaborn as sns
import argparse
from util.misc import choose_color
from Matching.Match import compute_matching_mapping, compute_signals_matching
from fim import fpgrowth
from operator import itemgetter, attrgetter, methodcaller


__author__ = 'bejar'


if __name__ == '__main__':
    window = 400

    print 'W=', int(round(window))

    parser = argparse.ArgumentParser()
    parser.add_argument('--batch', help="Ejecucion no interactiva", action='store_true', default=False)
    parser.add_argument('--exp', nargs='+', default=[], help="Nombre de los experimentos")

    args = parser.parse_args()
    lexperiments = args.exp

    if not args.batch:
       # 'e120503''e110616''e151126''e120511''e150514''e110906o''e150707'
        lexperiments = ['e150707']

    for expname in lexperiments:

        datainfo = experiments[expname]

        for ename in datainfo.expnames:
            print ename,
            try:
                f = open(datainfo.dpath + '/' + datainfo.name + '/Results/' + 'seqtrans-' + ename + '.csv.rul', 'r')

                count = np.zeros(5)
                double = np.zeros(5)
                llongrules = []
                for l in f:
                    rule = l.split('-1')
                    if len(rule) > 2:
                        count[len(rule)-3] += 1
                        for i in range(len(rule)-1):
                            if len(rule[i].strip().split(' ')) > 1:
                                double[len(rule[i].strip().split(' '))-2] += 1
                            if len(rule[i].strip().split(' ')) > 1:
                                llongrules.append(l.strip())

                f.close()
                print "Rules:", count, " Double", double
                for r in llongrules:
                    if len(r.split('-1')) > 3:
                        print '****************',
                    print r
            except IOError:
                pass
            print
            print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
