#!/usr/bin/env python

import sys, os, time, math
from JJnetstat import stataggregate
from JJnetstat import plothelper as nxplot
from DurandDB import blastq
    
if __name__ == "__main__":
    date = time.strftime('%Y%m%d')

    set_id = 91

    br_id = 87    # 8306 edges?
    # br_id = 89    low density? 0.0008 13810 edges
    # br_id = 90   60983 edges
    #br_id = 113  60983 edges

    #(stype, run_id, set_id_filter, symmetric, 'descr')
    networks = [ ('nc_score', 731, set_id, True, 'Scer'),
                 #('nc_score', 732, set_id, 'pre_post'),
                 #('nc_score', 733, set_id, 'post_post'),
                 ('nc_score', 734, set_id, True, 'ygob_four'),
                 ('nc_score', 735, set_id, True, 'ygob_nine'),
                 #('nc_score', 718, None, 'Scer_SP'),
                 ('bit_score', br_id, set_id, True, 'Scer'),
                 #('bit_score', 84, None, 'Scer_SP'),
                 ]
    thresholds = { 'nc_score': (0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35,
                                0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7,
                                0.75, 0.8, 0.85, 0.9, 0.95, 0.99
                                ),
                   'bit_score': (0, 20, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
                                 40, 50, 60, 70, 80,
                                 90, 100, 125, 150, 175, 200, 225, 250,
                                 275, 300, 325, 350, 375, 400, 425, 450, 475,
                                 500, 600, 700, 800, 900, 1000)
                   }

    # Calculate statistics for each network
    net_set = stataggregate.stat_set_debug( networks, thresholds)
    net_set.calculate_statistics( omit_spl=True, cacheq=True, verbose=False)

        
    g_plot = nxplot.nxplot( net_set.net_stat)
    #ftype = 'png'
    ftype = 'pdf'

    equiv_density_trans = g_plot.build_equiv_transform( 'graph_density',
                                                        equiv_runs=[('bit_score', br_id, set_id, True),
                                                                    ('nc_score', 735, set_id, True)])

    runs = net_set.net_stat.get_runs( stype_run_id=('bit_score', br_id, set_id, True))
    runs += net_set.net_stat.get_runs( stype_run_id=('nc_score', 731, set_id, True))
    runs += net_set.net_stat.get_runs( stype_run_id=('nc_score', 734, set_id, True))
    runs += net_set.net_stat.get_runs( stype_run_id=('nc_score', 735, set_id, True))
    
    # Draw plots
    for stat_name in ('ccomp_num',
                      'ccomp_num_single',
                      'ccomp_avg_size', 
                      'ccomp_avg_density_w',
                      'graph_mean_cc',
                      'graph_density',
                      # 'ccomp_avg_spl'
                      ):
        #g_plot.plot_statistic( stat_name, ftype=ftype, fprefix='figures/%s_ygob2' % date,
        #                       base_stype='nc_score')
        #g_plot.plot_statistic( stat_name, ftype=ftype, fprefix='figures/%s_ygob2_log' % date,
        #                       base_stype='nc_score',
        #                       top_transform=lambda a: math.log(a))
        if stat_name=='ccomp_num': legend_position='bottom'
        else: legend_position='bottomright'
        g_plot.plot_statistic( stat_name, ftype=ftype, fprefix='figures/%s_ygob2_eqd' % date,
                               base_stype='nc_score',
                               top_transform=equiv_density_trans,
                               absolute=True,
                               runs=runs, draw_title=False,
                               ylab=False,
                               ylog = stat_name == 'ccomp_avg_size',
                               #legend_position=legend_position,
                               legend_position='topright',
                               draw_legend = stat_name == 'ccomp_avg_size')


    #g_plot.plot_equivalent_x(('graph_density', #'ccomp_avg_size', 'ccomp_num_single',
    #                           #'graph_mean_cc', 'ccomp_num', 'ccomp_avg_density_w'
    #                           ),
    #                          equiv_runs=[('bit_score', br_id, set_id, True),
    #                                      ('nc_score', 735, set_id, True)],
    #                          fprefix='figures/%s_ygob2' % date,
    #                          xlimit=(29,1000), ylimit=(0,1))

    # # Select Density vs component size scatter plots
#     fprefix='figures/%s_sizedens' % date
#     for (bit_score, xlimit) in ((35, (0,800)),
#                                 (35, (0,3000)),
#                                 (60, (0,200)),
#                                 (80, (0,150)),
#                                 (100, (0,100))):
# 	run_bit = g_plot.net_stats.get_run( 'bit_score', 89, bit_score, 91)
# 	g_plot.plot_cc_scatter( run_bit, fprefix=fprefix+'_%s' % str(xlimit),
# 				xlimit=xlimit)
# 	# find the nearest equivalent nc_score
# 	equiv_nc = equiv_density_trans(bit_score)
# 	# if equiv_nc is None:
# 	#     print "no equivalent score for bit_score", bit_score
# 	#    continue
# 	print equiv_nc
# 	run_best = None
# 	runs = g_plot.net_stats.get_runs( stype_run_id=('nc_score', 735))
# 	for run in runs:
# 	    if run.set_id_filter != 91: continue
	    
# 	    if run_best is None or abs(run.min_score - equiv_nc) < abs(run_best.min_score - equiv_nc):
# 		run_best = run
# 	g_plot.plot_cc_scatter( run_best, fprefix=fprefix+'_%s' % str(xlimit),
# 				xlimit=xlimit)
    

    #for nc_id in net_stats:
    #    fprefix = 
    #    
    #    nxplot.plot_graph_statistics(net_stats[nc_id], plot_stats,
    #                                 fprefix=fprefix, ftype='png')
    #     
    #    nxplot.plot_graph_cc_histogram( net_stats[nc_id], 'ccomp_size',
    #                                    'nc_score', 0.3,
    #                                    xlimit=(0,600), ylimit=(0,10),
    #                                    fprefix=fprefix, ftype=ftype)#
    #
    #    nxplot.plot_graph_cc_histogram( net_stats[nc_id], 'ccomp_density',
    #                                    'nc_score', 0.3,
    #                                    xlimit=(0,600), ylimit=(0,10),
    #                                    fprefix=fprefix, ftype=ftype)

     #   # nxplot.plot_graph_cc_histograms(net_stats[nc_id], 'ccomp_size',
     #                                bitthresh=225, ncthresh=0.975,
     #                                xlimit=(0,600), ylimit=(0,10),
     #                                fprefix=fprefix, ftype=ftype)
     
     #nxplot.plot_graph_cc_histograms(net_stats, 'ccomp_density',
     #                                bitthresh=225, ncthresh=0.975,
     #                                fprefix=fprefix, ftype=ftype)

     #for (bitthresh,ncthresh) in ((29,0.05),(35,0.25),(40,0.4),(45,0.55),(50,0.65),
     #                             (55,0.7),(60,0.75),(65,0.8),(70,0.85),(80,0.85),
     #                             (90,0.9),(100,0.925),(125,0.95),(150,0.95),
     #                             (175,0.975),(200,0.975),(225,0.975),(250,0.975),
     #                             (300,0.999),(350,0.999),(400,0.999),(500,0.999),
     #                             (600,0.999),(700,0.999),(800,0.999),(900,0.999),
     #                             (1000,0.999)):
     #    nxplot.plot_graph_k_histograms(net_stats, 'degree_hist', bitthresh=bitthresh,
     #                                   ncthresh=ncthresh, fprefix=fprefix,ftype='png')
     #    nxplot.plot_graph_k_histograms(net_stats, 'cc_hist', bitthresh=bitthresh,
     #                                   ncthresh=ncthresh, fprefix=fprefix,ftype='png')


