def write_relationship_types(data, definition, def_args, out_dir):
    for year in data.all_years:
        with open(out_dir + '%s.csv' % year, 'wb') as csvfile:
            out_file = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
            out_file.writerow(['Country1', 'Country2', 'Link type', 'Export/Import ratio'])
            for (A, B) in countries.country_pairs(data.countries()):
                link_type = definition(data, year, A, B, def_args)
                if link_type != NO_LINK:
                    out_file.writerow([A, B, link_type, data.export_import_ratio(A, B, year)])
Пример #2
0
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()
Пример #3
0
def sign_distributions(data, year, definition, def_args):
    positive_edges_count, negative_edges_count, no_edge_count = 0, 0, 0
    for (A, B) in countries.country_pairs(data.countries()):
        link_type = definition(data, year, A, B, def_args)
        if link_type == NEGATIVE_LINK:
            negative_edges_count += 1
        elif link_type == POSITIVE_LINK:
            positive_edges_count += 1
        elif link_type == NO_LINK:
            no_edge_count += 1
    edges_count = positive_edges_count + negative_edges_count
    return edges_count, positive_edges_count, negative_edges_count, no_edge_count
def print_graph_densities_for_different_thresholds(data, thresholds, years):
    f = open(OUT_DIR.DEFINITION_D + 'combinations.txt', 'w')
    for T in thresholds:
        for year in years:
            positive_edges = 0
            negative_edges = 0
            for (A, B) in countries.country_pairs(data.countries()):
                link_sign = definition_D(data, year, A, B, args_for_definition_D(T, log_file=f))
                if link_sign == POSITIVE_LINK: positive_edges += 1
                if link_sign == NEGATIVE_LINK: negative_edges += 1
            N = 203
            density = 2.0 * (positive_edges + negative_edges) / (N * (N - 1)) * 100
            print "%d,%d,%f,%d,%d,%d" % (T, year, density, positive_edges, negative_edges, N)
    f.close()
def print_histogram_matlab_code(data, low, high):
    i = 0
    str = ""
    list = []
    for (A, B) in countries.country_pairs(data.countries()):
        total = data.total_exports_from_C1_to_C2(A, B)
        if total != 0 and low < total < high:
            str = "%s %d" % (str, total)
            list.append(total)
            i += 1
    print "y=[%s];" % str
    print "hist(y,100000);"
    print "xlabel('Export quantity');"
    print "ylabel('Count of country pairs');"
    print "set(gca, 'Xscale', 'log');"
    print "saveas(gcf,'definition_C_histogram','png');"
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 print_densities_for_thresholds(data, definition, T1_thresholds, T2_thresholds, log_file_name):
    f1 = open(OUT_DIR.DEFINITION_C + log_file_name + '-edges.txt', 'w')
    f2 = open(OUT_DIR.DEFINITION_C + log_file_name + '-traids.txt', 'w')
    f3 = open(OUT_DIR.DEFINITION_C + log_file_name + '-nodes.txt', 'w')
    countries_list = ['USA', 'Iran', 'Iraq', 'Argentina', 'UK', 'China', 'Kuwait', 'France,Monac']

    for pruning_T in T1_thresholds:
        for classifying_T in T2_thresholds:
            args1 = args_for_definition_C(pruning_T, classifying_T, f1)
            args2 = args_for_definition_C(pruning_T, classifying_T, f1)

            for year in range(1969, 2001):
                (positive_edges, negative_edges) = (0, 0)
                for (A, B) in countries.country_pairs(countries_list):
                    link_sign = definition(data, year, A, B, args1)
                    if link_sign == POSITIVE_LINK: positive_edges += 1
                    if link_sign == NEGATIVE_LINK: negative_edges += 1
                N = 203
                density = 2.0 * (positive_edges + negative_edges) / (N * (N - 1)) * 100
                print "%d,%f,%d,%f,%d,%d,%d" % (
                    pruning_T, classifying_T, year, density, positive_edges, negative_edges, N)
                for (A, B, C) in combinations(countries_list, 3):
                    linkAtoB = definition(data, year, A, B, args2)
                    linkBtoC = definition(data, year, B, C, args2)
                    linkCtoA = definition(data, year, C, A, args2)
                    triangle = [linkAtoB, linkBtoC, linkCtoA]
                    pcount = triangle.count(POSITIVE_LINK)
                    ncount = triangle.count(NEGATIVE_LINK)
                    mcount = triangle.count(NO_LINK)
                    f2.write("%d,%.2f,%d,%s,%s,%s,%s,%s,%s,T%d%d%d\n" % (
                        year, pruning_T, classifying_T, A, B, C, linkAtoB, linkBtoC, linkCtoA, pcount, ncount, mcount))
                for A in countries_list:
                    f3.write("%d,%s,%d\n" % (year, A, degree_sum(data, year, A, definition, args1)))
    f1.close()
    f2.close()
    f3.close()