def write_number_of_positive_traids(data, definition, def_args, out_dir): def relationship_def(data, year, A, B, def_args1): return definition(data, year, A, B, def_args) == POSITIVE_LINK all_years_data = {} years = data.all_years countries_list = data.countries() for year in years: single_year_data = {} with open(out_dir + '%s.csv' % year, 'wb') as csvfile: out_file = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) out_file.writerow(['Country', 'Number of positive traids']) relationship_matrix = get_relationship_matrix(data, year, relationship_def, {}) relationship_matrix_cube = __matrix_cube(relationship_matrix) for C in countries_list: number_of_positive_traids = number_of_traids_for_a_country(relationship_matrix_cube, C) out_file.writerow([C, number_of_positive_traids]) single_year_data[C] = number_of_positive_traids all_years_data[year] = single_year_data with open(out_dir + 'all-years.csv', 'wb') as csvfile: out_file = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) header = ['*'] for C in countries_list: header.append(C) out_file.writerow(header) for year in years: row = [year] for C in countries_list: row.append(all_years_data[year][C]) out_file.writerow(row)
def table1(data, year, definition, def_args): def link_exists_def(data, year, A, B, def_args1): if 'World' in [A, B]: return 0 return 0 if definition(data, year, A, B, def_args) == NO_LINK else 1 edges_count, positives, negatives, _ = sign_distributions(data, year, definition, def_args) return {'Name': 'Table1', 'Nodes': len(data.countries()), 'Edges': edges_count, '+ edges': positives * 100.0 / edges_count, '- edges': negatives * 100.0 / edges_count, 'Traids': number_of_traids(__matrix_cube(get_relationship_matrix(data, year, link_exists_def, {}))) }