#!/usr/bin/env python
# coding=utf-8
from Separation_Gen import Separation_Gen
from utils.make_tree import gen_gh_trees
from utils.read_data import read_data, read_tree
from utils.save_result import save_to_file
import random
import sys
import copy
import pdb

# set k=10, l=5 as default!


def get_result_one(att_trees, data, K=10, L=5):
    "fix K=10, L=5"
    print "K=%d" % K
    print "L=%d" % L
    data_back = copy.deepcopy(data)
    result, eval_result = Separation_Gen(att_trees, data, K, L)
    data = copy.deepcopy(data_back)
    print "RNCP %0.2f" % eval_result[0] + "%"
    print "TNCP %0.2f" % eval_result[1] + "%"
    print "Running time %0.2f" % eval_result[2] + "seconds"
    # save_to_file((att_trees, data, result, K, L))


def get_result_K(att_trees, data, L=5):
    "change K, while fixing L"
    print "L=%d" % L
    data_back = copy.deepcopy(data)
    for K in range(5, 55, 5):
        print '#' * 30
        print "K=%d" % K
        result, eval_result = Separation_Gen(att_trees, data, K, L)
        data = copy.deepcopy(data_back)
        print "RNCP %0.2f" % eval_result[0] + "%"
        print "TNCP %0.2f" % eval_result[1] + "%"
        print "Running time %0.2f" % eval_result[2] + "seconds"
        # save_to_file((att_trees, data, result, K, L))


def get_result_L(att_trees, data, K=10):
    "change L, while fixing K"
    print "K=%d" % K
    data_back = copy.deepcopy(data)
    for L in range(5, 55, 5):
        print '#' * 30
        print "L=%d" % L
        result, eval_result = Separation_Gen(att_trees, data, K, L)
        data = copy.deepcopy(data_back)
        print "RNCP %0.2f" % eval_result[0] + "%"
        print "TNCP %0.2f" % eval_result[1] + "%"
        print "Running time %0.2f" % eval_result[2] + "seconds"
        # save_to_file((att_trees, data, result, K, L))


def get_result_dataset(att_trees, data, K=10, L=5, n=10):
    "fix k and l, while changign dataset size"
    data_back = copy.deepcopy(data)
    length = len(data_back)
    print "K=%d" % K
    print "L=%d" % L
    joint = 5000
    h = length / joint
    if length % joint == 0:
        h += 1
    for i in range(1, h + 1):
        pos = i * joint
        srncp = stncp = rtime = 0
        if pos > length:
            continue
        print '#' * 30
        print "size of dataset %d" % pos
        for j in range(n):
            temp = random.sample(data, pos)
            result, eval_result = Separation_Gen(att_trees, temp, K, L)
            srncp += eval_result[0]
            stncp += eval_result[1]
            rtime += eval_result[2]
            data = copy.deepcopy(data_back)
            # save_to_file((att_trees, temp, result, K, L))
        srncp /= n
        stncp /= n
        rtime /= n
        print "Average RNCP %0.2f" % srncp + "%"
        print "Average TNCP %0.2f" % stncp + "%"
        print "Running time %0.2f" % rtime + "seconds"
        print '#' * 30


if __name__ == '__main__':
    FLAG = ''
    LEN_ARGV = len(sys.argv)
    try:
        FLAG = sys.argv[1]
    except:
        pass
    # read record
    K = 10
    L = 5
    print '*' * 30
    # make generalization hierarchies
    gen_gh_trees()
    # read gentree tax
    ATT_TREES = read_tree()
    # read record
    DATA = read_data()
    # Separation_Gen need only GH for transaction
    if FLAG == 'k':
        get_result_K(ATT_TREES, DATA)
    elif FLAG == 'l':
        get_result_L(ATT_TREES, DATA)
    elif FLAG == 'data':
        get_result_dataset(ATT_TREES, DATA)
    elif FLAG == 'one':
        if LEN_ARGV > 2:
            K = int(sys.argv[2])
            L = int(sys.argv[3])
            get_result_one(ATT_TREES, DATA, K, L)
        else:
            get_result_one(ATT_TREES, DATA)
    elif FLAG == '':
        get_result_one(ATT_TREES, DATA)
    else:
        print "Usage: python anonymizer [k | l | data | one]"
    print "Finish 1M_Generalization!!"
    print '*' * 30

@@ -1416,32 +1416,29 @@
  L in range(
-5, 55, 5
+2, 16
 ):%0A        p

Modify get_result_L with [2,15] test range.