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()
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)
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, POSITIVE_LINK, NEGATIVE_LINK from project.signed_networks.structural_balance.metrics.network import print_table, table1, link_type_ratio, traid_type_ratio data = ExportData() data.load_file('../../' + WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, should_read_world_datapoints=True) year = 2000 definition = definition_C3 def_args1000 = args_for_definition_C(10, 1000) def_args5000 = args_for_definition_C(10, 5000) # print_table(table1(data, year, definition, def_args1000)) def print_code_for_sign_distribution_over_time(): print "x=%s;" % str([year for year in data.all_years]).replace(",", " ") # print "p1000=%s;" % str([link_type_ratio(data, year, definition, def_args1000, POSITIVE_LINK) # for year in data.all_years]).replace(",", " ") print "p5000=%s;" % str([link_type_ratio(data, year, definition, def_args5000, POSITIVE_LINK) for year in data.all_years]).replace(",", " ") # print "n1000=%s;" % str([link_type_ratio(data, year, definition, def_args1000, NEGATIVE_LINK) # for year in data.all_years]).replace(",", " ") print "n5000=%s;" % str([link_type_ratio(data, year, definition, def_args5000, NEGATIVE_LINK) for year in data.all_years]).replace(",", " ") print "plot(x,p5000,x,n5000);" print "legend('positive edges','negative edges')" print "saveas(gcf,'sign-distribution-over-time(Not-density)','png');"
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))
print "saveas(gcf,'individual-traid-embeddedness-%s-all','png');" % year print "plot(x,t1p,'m-*',x,t1n,'r-o',x,t2p,'g-*',x,t2n,'b-o');" print "xlabel('Number of common neighbours');" print "ylabel('Percentage of triangles');" print "legend('T1+','T1-','T2+','T2-');" print "saveas(gcf,'individual-traid-embeddedness-%s-t1-and-t2','png');" % year def embeddedness_vs_positive_edges_over_time(def_args): print "x=%s" % (str(common_neighbours_count_range).replace(",", " ")) for year in data.all_years: fractions = fraction_of_embedded_positive_signs(data, year, definition_C3, def_args) (_, positive_edges, negative_edges) = count_edge_types(data, year, definition_C3, def_args) randomNetwork = RandomNetwork(positive_edges, negative_edges, definition_C3, def_args) random_fractions = fraction_of_embedded_positive_signs(data, year, randomNetwork.link_sign, def_args) print "actual=%s" % (str([fractions[i] for i in common_neighbours_count_range]).replace(",", " ")) print "random=%s" % (str([random_fractions[i] for i in common_neighbours_count_range]).replace(",", " ")) print "plot(x,actual,'b-*',x,random,'r-*');" print "saveas(gcf,'link-embeddedness-%s','png');" % year #traid_counts = traids_per_common_edge_count(data, year, definition_C3, args_for_definition_C(10, 5000)) # #for i in range(0, max_common_neighbours_possible + 1): # (t0, t1, t2, t3) = traid_counts[i] # print "%d\t%d\t%d\t%d\t%d" % (i, t0, t1, t2, t3) # embeddedness_vs_positive_edges_over_time(args_for_definition_C(10, 1000)) # embeddedness_vs_balanced_traids_over_time(args_for_definition_C(10, 1000)) embeddedness_vs_individual_traids_over_time(args_for_definition_C(10, 5000))
self.export_quantity = [] def add_data(self, year, p, n, ex): self.positive_links_count.append([year, p]) self.negative_links_count.append([year, n]) self.export_quantity.append([year, ex]) all_nations_dict = [] json_file = open(out_dir + 'nations_gen.json', 'w') for A in countries_list: nation = NationData(A) for year in data.all_years: positive = 0 negative = 0 for B in countries_list: if A == B: continue link_type = definition(data, year, A, B, def_args) if link_type == NEGATIVE_LINK: negative += 1 elif link_type == POSITIVE_LINK: positive += 1 nation.add_data(year, positive, negative, data.total_exports(A, year)) all_nations_dict.append(nation.__dict__) json_file.write(json.dumps(all_nations_dict)) json_file.close() data = ExportData() data.load_file('../../' + WORLD_TRADE_FLOW_DATA_FILE_ORIGINAL, should_read_world_datapoints=True) positives_and_negatives_data_d3(data, definition_C3, args_for_definition_C(10,1000), OUT_DIR.POSITIVES_AND_NEGATIVES)