예제 #1
0
 def __init__(self, year, method, flg_sym, norm="norm", is_gcc=False):
     self.year = year
     if flg_sym:
         self.flg_sym = 'sym'
     else:
         self.flg_sym = ''
     self.G = nm.construct_ntwrkX_Graph(self.dirPre, self.year,
                                        self.flg_sym)
     self.gcc = max(nx.connected_components(self.G), key=len)
     self.num_gcc = len(self.gcc)
     self.trade_ntwrk_graph, self.imports, self.exports =\
         dm.load_adjacency_npz_year(self.dirIn, year, self.num_countries,
                                    self.flg_sym)
     assert np.any(np.sum(self.trade_ntwrk_graph, axis=0) ==
                   self.imports), 'Imports are Weird'
     assert np.any(np.sum(self.trade_ntwrk_graph, axis=1) ==
                   self.exports), 'Exports are Weird'
     if method is "Laplacian":
         print('hi')
         self.trade_ntwrk = nm.networkX_laplacian(self.G, self.flg_sym,
                                                  norm)
     else:
         self.trade_ntwrk = nm.construct_ntwrk_method(
             self.trade_ntwrk_graph, method)
     if is_gcc:
         self.trade_ntwrk = nm.convert_adjacency_to_giant_component(
             self.G, self.trade_ntwrk)
     self.labels = None
# from mpl_toolkits.mplot3d import Axes3D
# import time
# import os
# import csv

# import sys

# import scipy as sp       # library to deal with sparse graphs for Cuthill-Mckee and Laplacian

#------------------------------------------------------------------------------------------------------------
# Load in a network for a specific year

dirPre = dm.set_dir_tree()
year = np.array(1962)
flg_sym = True
G = nm.construct_ntwrkX_Graph(dirPre=dirPre, year=year, flg_sym=flg_sym)

#------------------------------------------------------------------------------------------------------------
# Explore 'community' module
# Compute best partition and dendrogram using Louvain algorithm in 'community' module

res = [0.1, 0.5, 1, 3, 5, 7,
       10]  # different resolution values for partitioning algorithms

q_bp = np.zeros_like(res)
q_dend = np.zeros((3, len(res)))
coverage = np.zeros_like(res)

for i, r in enumerate(res):
    print('Resolution is ', r)
    #
예제 #3
0
import numpy as np
import pandas as pd
import networkx as nx
import pickle
import utils.data_manipulation as dm
import utils.network_manipulation as nm

# This script Construct directed network Graph object that NetworkX can analyze from the Adjacency matrix
# loaded from an .npz file. It will save the graph object as a .gpickle file. It will also save output
# figures of the Adjacency Matrix, and the network overlayed on a map.

sym = 'sym'  # 'sym' if undirected or '' if directed.

#------------------------------------------------------------------------------------------------------------
## (0). Check what Operating System you are on (either my machine or Cortex cluster) and adjust directory structure accordingly.
dirPre = dm.set_dir_tree()

#------------------------------------------------------------------------------------------------------------
## (1). Load country names that align with 3 letter acronyms used in origin destination file
countries = dm.load_country_lat_lon_csv(dirPre)
num_countries = countries.shape[0]

### Loop over each year
years = range(1962, 2015)
# For each year: Each trade network from years 1962 - 2014.
for year in years:
    print(str(year))

    # Construct a networkX graph containing node and edge attributes.
    trade_ntwrkG = nm.construct_ntwrkX_Graph(dirPre, year, flg_sym)
예제 #4
0
conts = set(continent)  # this is a 'set object' (all the different countries)
conts = list(conts)  # convert 'set object' to a list that I can iterate over.
conts = np.sort(conts)
node_colors_by_continent = np.zeros(len(continent))
for i in range(0, len(conts)):
    node_colors_by_continent[np.array(continent == conts[i])] = i

# (-) Loop through different years and compute modularity using NetworkX
for year in range(1962, 1963):

    ## (-) First the adjacency matrix is loaded from the adj_npz.
    # Then the adj_matrix is converted into a NetworkX DiGraph object.
    # Finally the DiGraph is used to create a laplacian matrix, using the  NetworkX
    # laplacian_matrix functions, selected in our network manipulation suit.
    adj_npz, _, _ = dm.load_adjacency_npz_year(dirIn, year, num_countries, sym)
    adj_graph = nm.construct_ntwrkX_Graph(dirPre, year, sym)
    lap_ntwkX = nm.networkX_laplacian(adj_graph, sym, norm, weight)

    # (-) Uncomment these lines to check if Our implemented modularity matches results of NetworkX version.
    # dirLap = str(dirPre + 'laplacian_ntwrk_npz_files/')
    # lap_npz = dm.load_laplacian_npz_year(dirLap, year, num_countries, sym)
    # diff = lap_ntwkX - lap_npz
    # claim = (diff).any()
    # print('Claim differences in 2 laplacian calcs.', claim )
    # if claim:
    # 	plt.imshow(diff)
    # 	plt.colorbar()
    # 	plt.show()
    if False:
        np.savez(str(dirOut + file_name + str(year) + '_' +
                     str(num_countries) + 'countries.npz'),