from D_relation import gen_adj, calc_adj
from dat.relation_tgc.relation_tgc import gen_adj_tgc
from A_Global import args, path_dat, path_relation
from D_basket import *


def calc_relation_intersec_rate(rset1, rset2):
    rset1   = set(rset1)
    rset2   = set(rset2)

    return round((len(rset1.intersection(rset2))) / len(rset1), 3), \
           round((len(rset2.intersection(rset1))) / len(rset2), 3)


'''
解析 TGC 论文提供的 json 数据，获取"股票-行业"关系数据
[stock, stock, n_relation] 二维矩阵
行业关系在关联性关系的占比
关联性关系在行业关系的占比
一些统计信息
双方的热力图
'''
if __name__ == '__main__':
    market                          = 'NYSE'
    # market                          = 'NASDAQ'

    args['market']                  = market
    threshold_discrete_dict         = {
        'r1d'   : 0.000,
        'r5d'   : 0.000,
        'r20d'  : 0.000,
        'r60d'  : 0.000,
    }
    args['threshold_discrete_dict'] = threshold_discrete_dict
    args['min_support_same']        = 0.6
    args['min_support_diff']        = 0.6

    #
    calc_threshold_rate(market)

    # basket
    get_basket(market, threshold_discrete_dict)
    calc_threshold_rate(market)

    # relation
    # relation_set, relation_dict, adj = gen_adj(args)
    relation_set, relation_set, relation_dict, adj = calc_adj(args)
    print(len(relation_set))
    # res = np.load(path_relation / path_file_adj)
    print(sum(adj))
    # print(sum(res))
    # print(adj == res)

    tgc_relation_set, tgc_relation_dict, tgc_adj    = gen_adj_tgc(market)
    print(len(tgc_relation_set))

    # ratio_instersection
    ratio_tgc, ratio_rule                           = calc_relation_intersec_rate(tgc_relation_set, relation_set)
    print(ratio_tgc, ratio_rule)


'''
调整各个阈值，尽量增大两个占比 - 注意 TGC 文章因为直接关系太少，所以使用了二阶关系
流程写论文，列出各项阈值，最好能加上算法伪代码
分析各个列对tgc关系的占比 - 实验部分分析

离散化不设阈值，改代码能分别调整正负关系的min_support，现在应该是不能，两个都一样，好像都用same的
'''