from project.config import WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL
from project.export_data.exportdata import ExportData
from project.signed_networks.definitions import definition_C3, args_for_definition_C
from project.signed_networks.structural_balance.metrics.vertex import degree_sum, degree_count, positive_edge_count, negative_edge_count
from project.util import file_safe

data = ExportData()
data.load_file('../../' + WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, should_read_world_datapoints=True)

definition = definition_C3
def_args1 = args_for_definition_C(10, 5000)
def_args2 = args_for_definition_C(10, 1000)


def print_degree_sum_over_time(args, other_args, this, other):
    print "x=%s;" % str([year for year in data.all_years]).replace(",", " ")
    for A in data.countries():
        print "degreecount=%s;" % str([degree_count(data, year, A, definition, args) for year in data.all_years]).replace(",", " ")
        print "degreesum1=%s;" % str([degree_sum(data, year, A, definition, args) for year in data.all_years]).replace(",", " ")
        print "positivecount1=%s;" % str([positive_edge_count(data, year, A, definition, args) for year in data.all_years]).replace(",", " ")
        print "negativecount1=%s;" % str([negative_edge_count(data, year, A, definition, args) for year in data.all_years]).replace(",", " ")
        print "degreesum2=%s;" % str([degree_sum(data, year, A, definition, other_args) for year in data.all_years]).replace(",", " ")
        print "plot(x,degreesum1,'b-o',x,degreesum2,'b-*',x,degreecount,'m-o',x,positivecount1,'g-o',x,negativecount1,'r-o');"
        print "hline = refline([0 0]);"
        print "set(hline,'Color','b');"
        print "legend('degree-sum1(T2=%d)','degree-sum2(T2=%d)','degree-count','positivecount1','negativecount1','Location','Best')" %(this,other)
        print "saveas(gcf,'%s-%d','png');" % (file_safe(A),this)


print_degree_sum_over_time(def_args1, def_args2, 5000, 1000)
print_degree_sum_over_time(def_args2, def_args1, 1000, 5000)
    if n == 0: return 1
    if n < 1:
        n = 1 / n
    return n - 1


def generate_network_graph_data(data, year, subset_of_countries, out_file, definition, args):
    f = open(out_file, 'w')
    f.write(html_header())
    for (c1, c2) in country_pairs(subset_of_countries):
        link_type = definition(data, year, c1, c2, args)
        if link_type != NO_LINK:
            ratio = data.export_import_ratio(c1, c2, year)
            f.write('{source:"%s", target:"%s", type:"%s",repulsionpercentage:"%f"},\n' % (
                c1, c2, link_type, normalize(ratio)))

    f.write(html_footer(year))
    f.close()


data = ExportData()
data.load_file('../' + WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, should_read_world_datapoints=True)

for year in range(1968, 2001):
    top_countries_and_exports = data.top_countries_by_export(year, 50)
    # top_countries_and_exports = data.countries()
    generate_network_graph_data(data, year, top_countries_and_exports, output_file_html(year),
                                definition_C3,
                                args_for_definition_C(10, 5000))

def print_missing_links_db(data, year, T, log_file_name):
    two_way_args = args_for_definition_D(T)
    one_way_args = args_for_definition_D(T, mode='one-way')
    f = open(OUT_DIR.DEFINITION_D + log_file_name + ".%d.txt" % T, 'w')
    for (A, B) in countries.country_pairs(data.countries()):
        if definition_D(data, year, A, B, two_way_args) == NO_LINK:
            one_way = definition_D(data, year, A, B, one_way_args)
            other_way = definition_D(data, year, B, A, one_way_args)
            f.write("Y%d,%s,%s,%s,%s\n" % (year, file_safe(A), file_safe(B), one_way, other_way))
            if one_way != other_way:
                for Y in range(1963, 2001):
                    f.write("%d,%s,%s,%.4g,%.4g\n" % (
                        Y, file_safe(A), file_safe(B),
                        data.export_data_as_percentile(Y, A, B),
                        data.export_data_as_percentile(Y, B, A)))
    f.close()


data = None
data = ExportData()
data.load_file('../' + WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, should_read_world_datapoints=True)

#generate_matlab_histogram_code(data, [99], [2000], ['Georgia', 'USA'])
print_histogram_as_text(data, range(1963, 2001), data.countries())
#print_graph_densities_for_different_thresholds(data, thresholds, a_few_years)
print_missing_links_db(data, 2000, 90, 'def_d_histogram_pair_wise')
print_missing_links_db(data, 2000, 99, 'def_d_histogram_pair_wise')


                continue
            if not trade_relationship_exists(exporter, importer):
                continue
            print exporter + ' ' + importer
            print str(slope_data(exporter, importer)) + ' ' + str(data.export_data(2000, exporter, importer))
            f.write(str(slope_data(exporter, importer)) + ' ' + str(data.export_data(2000, exporter, importer)) + '\n')
            if importer == 'World':
                f_world.write(
                    str(slope_data(exporter, importer)) + ' ' + str(data.export_data(2000, exporter, importer)) + '\n')

        f.close()
        f_world.close()
    f_countries_list.close()
    return total_countries

data = ExportData()
data.load_file(WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, ["Value00"], should_read_world_datapoints=True)

total_countries = write_data_files_for_slope_vs_export_plots(data,'matlab', 'out/slope-vs-export-percent')
matlab_program_file = open('matlab/slope_vs_export_percent_gen.m', 'w')

matlab_program_file.write("clear" + '\n')
matlab_program_file.write("total = " + str(total_countries) + '\n')
matlab_program_file.write(
    "[country_names,all_countries,only_world]=textread('out/slope-vs-export-percent/all-countries.txt','%s %s %s' ,total)" + '\n')
matlab_program_file.write("" + '\n')
matlab_program_file.write("for i=1:total," + '\n')
matlab_program_file.write("    all_countries{i}" + '\n')
matlab_program_file.write("    all_data = load(all_countries{i})" + '\n')
matlab_program_file.write("    world_data = load(only_world{i})" + '\n')
matlab_program_file.write("" + '\n')
from project.config import  WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL
from project.export_data import strongties
from project.export_data.exportdata import ExportData
from project.export_data.strongties import is_there_a_strong_tie_method_B, strong_tie_def_args
from project.traids_vs_degree_plot import config
from project.traids_vs_degree_plot.csv_writer.csv_writer import csv_write


def write_out_graph_data_for_traids_vs_degree_plot(data):
    for year in data.all_years:
        matrix_for_a_year = strongties.get_relationship_matrix(data, year, is_there_a_strong_tie_method_B,
            strong_tie_def_args(config.STRONG_TIES_LOWER_BOUND, config.STRONG_TIES_UPPER_BOUND))
        csv_write(config.graph_data_file_name(year), strongties.graph_data(matrix_for_a_year))

data = ExportData()
data.load_file(WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL)
write_out_graph_data_for_traids_vs_degree_plot(data)




from itertools import combinations
from project.config import WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL
from project.export_data.exportdata import ExportData
from project.signed_networks.definitions import definition_C3, args_for_definition_C

data = ExportData()
data.load_file('../../' + WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, should_read_world_datapoints=True)

year = 1975
definition = definition_C3
args = args_for_definition_C(10, 5000)

for (A, B) in combinations(data.countries(), 2):
    print "%s,%s,%s" % (A, B, definition(data, year, A, B, args))