def write_data_files_for_slope_vs_export_plots(data,root_dir, out_dir):
    f_countries_list = open(root_dir + '/' + out_dir + '/all-countries.txt', 'w')

    total_countries = 0
    for exporter in countries.countries:
        all_countries_file_name = out_dir + '/' + file_safe(exporter) + '.txt'
        world_file_name = out_dir + '/' + file_safe(exporter) + '-world.txt'

        f = open(root_dir + '/' + all_countries_file_name, 'w')
        f_world = open(root_dir + '/' + world_file_name, 'w')
        if not exporter == 'World':
            f_countries_list.write(file_safe(exporter) + ' ' + all_countries_file_name + ' ' + world_file_name + '\n')
            total_countries += 1
        for importer in countries.countries:
            if exporter == importer:
                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
def generate_triad_type_transition_matlab_code(data, definition, def_args):
    f = open(OUT_DIR.DIR + 'triad-matlab-code.txt', 'w')

    for (A, B, C) in combinations(data.countries(), 3):
        f.write("%s-%s-%s:y=%s\n" % (file_safe(A), file_safe(B), file_safe(C),
                                     str([traid_plot_value(
                                         triad_type(data, year, A, B, C, definition, def_args))
                                          for year in data.all_years]).replace(",", " ")))
    f.write("x=%s;\n" % str([year for year in data.all_years]).replace(",", " "))
    f.write("plot(x,y,'b-o',[2000 2000],[0 8],'b.');\n")
    f.write("set(gca,'YTick',[0 1 2 3 4 5 6 7 8 9]);\n")
    f.write("set(gca,'YTickLabel',{'T003','T012','T102','T021','T111','T201','T030','T120','T210','T300'});\n")

    f.close()
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()
def gen_intermediate_data_for_both_way_linear_regression_plot(data,f1, f2, f3):
    out1 = open(f1, 'w')
    out2 = open(f2, 'w')
    i = 0
    for exporter in countries.countries:
        subdir = 'in/wtf/' + file_safe(exporter) + '/'
        for importer in countries.countries:
            if importer == exporter:
                continue
            filepath = subdir + 'exports-to-' + file_safe(importer) + '.txt'
            if not os.path.exists(rootdir + subdir):
                os.makedirs(rootdir + subdir)
            file_non_empty = False
            with open(rootdir + filepath, 'wb') as csvfile:
                writer = csv.writer(csvfile, delimiter='\t')
                for y in YEAR_COLUMNS:
                    year = column_to_year(y)
                    a1 = float(data.export_data(year, exporter, importer))
                    a2 = data.total_exports(exporter, year)
                    a3 = float(data.export_data(year, importer, exporter))
                    a4 = data.total_exports(importer, year)
                    print "In %d, %s to %s: %f/%f, %s to %s : %f/%f" % (
                        year, exporter, importer, a1, a2, importer, exporter, a3, a4)
                    if a1 == 0.0 and a3 == 0.0:
                        continue
                    file_non_empty = True
                    export_percentage = 0 if a2 == 0 else a1 / a2 * 100
                    import_percentage = 0 if a4 == 0 else a3 / a4 * 100
                    writer.writerow([year, export_percentage, import_percentage])
            if file_non_empty:
                out1.write(filepath + "\n")
                out2.write(
                    "out/wtf/" + file_safe(exporter) + "/" + file_safe(exporter) + "-export-to-" + file_safe(
                        importer) + "\n")
                print i
                i += 1
            else:
                os.remove(rootdir + '/' + filepath)
                pass

    pass
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)
def trade_relationship_exists(exporter, importer):
    if file_safe(exporter) in slopes:
        if file_safe(importer) in slopes[file_safe(exporter)]:
            return True
    return False
def slope_data(exporter, importer):
    return slopes[file_safe(exporter)][file_safe(importer)]
def __log_to_file(T2, args, country_A, country_B, one_way, other_way, year):
    if args['f'] is not None:
        args['f'].write(
            "%d,%s,%s,%d,%s,%s\n" % (year, file_safe(country_A), file_safe(country_B), T2, one_way, other_way))
def gen_intermediate_data_for_one_way_linear_regression_plot(data,input_file, f1, f2, f3):
    i = 0
    reader = csv.DictReader(open(input_file, 'rb'), skipinitialspace=True)
    out1 = open(f1, 'w')
    out2 = open(f2, 'w')
    out3 = open(f3, 'w')
    for row in reader:
        importer = row.get('Importer')
        exporter = row.get('Exporter')
        if importer == exporter or exporter == 'World':
            continue
        if not is_valid_country(importer) or not is_valid_country(exporter):
            continue
        subdir = 'in/wtf/' + file_safe(exporter) + '/'
        filepath = subdir + 'exports-to-' + file_safe(importer) + '.txt'

        if not os.path.exists(rootdir + subdir):
            os.makedirs(rootdir + subdir)
        with open(rootdir + filepath, 'wb') as csvfile:
            writer = csv.writer(csvfile, delimiter='\t')
            for column in YEAR_COLUMNS:
                export_quantity = row.get(column)
                if export_quantity == 'NaN':
                    continue
                year = column_to_year(column)
                print exporter + ' ' + importer + ' ' + str(year) + ' ' + export_quantity + ' ' + str(data.total_exports(
                    exporter, year))
                writer.writerow([year, float(export_quantity) / data.total_exports(exporter, year) * 100])

        out1.write(filepath + "\n")
        out2.write(
            "out/wtf/" + file_safe(exporter) + "/" + file_safe(exporter) + "-export-to-" + file_safe(importer) + "\n")
        print i
        i += 1

    for c in countries.countries:
        out3.write("mkdir('out/wtf/" + file_safe(c) + "')\n")

    out3.write("clear\n")
    out3.write("total = " + str(i) + "\n")
    out3.write("inputfile123=textread('input-files-percent.txt','%s',total)\n")
    out3.write("outputfile123=textread('output-files-percent.txt','%s',total)\n")
    out3.write("\n")
    out3.write("for i=1:total,\n")
    out3.write("    data = load(inputfile123{i})\n")
    out3.write("    datasize = size(data)\n")
    out3.write("    isemptyfile = datasize(1) == 0\n")
    out3.write("    if isemptyfile\n")
    out3.write("        data = [0 0]\n")
    out3.write("    end\n")
    out3.write("    x = data(:,1)\n")
    out3.write("    y = data(:,2)\n")
    out3.write("    ylinearfit = polyval(polyfit(x,y,1),x)\n")
    out3.write("    yquadfit = polyval(polyfit(x,y,2),x)\n")
    out3.write("    plot(x,y,'k-s',x,ylinearfit,x,yquadfit)\n")
    out3.write("    if isemptyfile\n")
    out3.write("        xlabel('No data')\n")
    out3.write("        ylabel('No data')\n")
    out3.write("    else\n")
    out3.write("        xlabel('Year')\n")
    out3.write("        ylabel('Export Quantity')\n")
    out3.write("    end\n")
    out3.write("    saveas(gcf,outputfile123{i},'png')\n")
    out3.write("    i\n")
    out3.write("end\n")