In [1]:
from gos import World, Neighborhood
from gos import visualization 
import matplotlib
import pandas as pd

In [2]:
def file_path(name):
    """
    Shortcut function to get the relative path to the directory
    which contains the data.
    """
    return "./examples/multiscale-migration/data/%s" % name

In [3]:
column_names = ["Name", "Code"]

def country_codes():
    """
    Build country rows from their names, ISO codes, and Numeric
    Country Codes.
    """
    cc = (
        pd.read_csv(
            file_path(
                "Country_List_ISO_3166_Codes_Latitude_Longitude.csv"),
            usecols=[0, 2, 3],
            index_col=1,
            keep_default_na=False))
    cc.columns = column_names
    return cc

def other_codes():
    other_codes = pd.read_csv(file_path("other.csv"), index_col=1)
    other_codes.columns = column_names[0:1]
    return other_codes

world = World(index=set(country_codes().index) | set(other_codes().index))
Neighborhood.update(country_codes().groupby("Alpha-3 code")["Name"].apply(list).to_dict())
Neighborhood.update(other_codes().groupby('ISO')["Name"].apply(list).to_dict())
Neighborhood.update(country_codes().groupby("Alpha-3 code")["Code"].apply(list).to_dict())

In [4]:
population = pd.read_csv(file_path("newPOP.csv"))
population = population.set_index("Country")

world.update_neighborhoods(population)


Could not find:  ['Faroe Isladns', 'Federated States of microstesa', 'Isle of man', 'Saint Martin', 'Northern Mariana', 'Saint Maarten']
Dropped:  [None, None, None, None, None, None]
Not found in other sets:  ['SGS', 'ESH', 'SXM', 'IMN', 'CCK', 'NFK', 'COK', 'BVT', 'SJM', 'GGY', 'UMI', 'MTQ', 'GUF', 'MNP', 'BES', 'FLK', 'HMD', 'FSM', 'MSR', 'WLF', 'JEY', 'AIA', 'ATA', 'IOT', 'TWN', 'PCN', 'GLP', 'VAT', 'FRO', 'SHN', 'CXR', 'SPM', 'TKL', 'MYT', 'REU', 'PSE', 'NIU', 'ANT', 'ATF']
Out[4]:
Population
ABW 1.016690e+05
AFG 2.880317e+07
AGO 2.336913e+07
ALB 2.913021e+06
AND 8.444900e+04
ARE 8.270684e+06
ARG 4.122389e+07
ARM 2.877311e+06
ASM 5.563700e+04
ATG 9.466100e+04
AUS 2.203175e+07
AUT 8.363404e+06
AZE 9.054332e+06
BDI 8.766930e+06
BEL 1.089559e+07
BEN 9.199259e+06
BFA 1.560522e+07
BGD 1.521491e+08
BGR 7.395599e+06
BHR 1.240862e+06
BHS 3.608320e+05
BIH 3.722084e+06
BLR 9.490583e+06
BLZ 3.216080e+05
BMU 6.512400e+04
BOL 9.918242e+06
BRA 1.967963e+08
BRB 2.795690e+05
BRN 3.886620e+05
BTN 7.276410e+05
... ...
TCD 1.188720e+07
TGO 6.502952e+06
THA 6.720881e+07
TJK 7.641630e+06
TKM 5.087210e+06
TLS 1.109591e+06
TON 1.041370e+05
TTO 1.328100e+06
TUN 1.063993e+07
TUR 7.232691e+07
TUV 1.053100e+04
TZA 4.609859e+07
UGA 3.391513e+07
UKR 4.587070e+07
UNK 1.775680e+06
URY 3.374415e+06
USA 3.093482e+08
UZB 2.856240e+07
VCT 1.093150e+05
VEN 2.902803e+07
VGB 2.722400e+04
VIR 1.062670e+05
VNM 8.693250e+07
VUT 2.362950e+05
WEGZ 3.811102e+06
WSM 1.862050e+05
YEM 2.360678e+07
ZAF 5.097943e+07
ZMB 1.385003e+07
ZWE 1.408632e+07

210 rows × 1 columns


In [5]:
un_pd = pd.read_excel(
        file_path(
            "UN_MigrantStockByOriginAndDestination_2015.xlsx"
        ),
        skiprows=15
    )
un_pd = un_pd.set_index('Unnamed: 1')
un_pd = un_pd.iloc[0:275,7:250]

# TODO: Should we be using the UN numbers for this?
un_pd = un_pd.sort_index().fillna(1)
world.add_matrix("un", un_pd)


Could not find:  ['Africa', 'Asia', 'Australia and New Zealand', 'Caribbean', 'Central America', 'Central Asia', 'Channel Islands', 'Developed regions', 'Developing regions', 'Eastern Africa', 'Eastern Asia', 'Eastern Europe', 'Europe', 'Latin America and the Caribbean', 'Least developed countries', 'Less developed regions excluding least developed countries', 'Melanesia', 'Middle Africa', 'Northern Africa', 'Northern America', 'Northern Europe', 'Oceania', 'Polynesia', 'South America', 'South-Eastern Asia', 'Southern Africa', 'Southern Asia', 'Southern Europe', 'Sub-Saharan Africa', 'WORLD', 'Western Africa', 'Western Asia', 'Western Europe']
Could not find:  ['Channel Islands']
Out[5]:
AFG ALB DZA ASM AND AGO ATG ARG ARM ABW ... USA VIR URY UZB VUT VEN VNM YEM ZMB ZWE
AFG 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 751.0 1.0 1.0 1.0 1.0 1.0 1.0
ALB 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 2628.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
DZA 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1968.0 1.0 1.0 1.0 1.0 1.0 1.0 4257.0 1.0 1.0
ASM 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 2516.0 1.0 1.0 1.0 1.0 1.0 209.0 1.0 1.0 1.0
AND 1.0 1.0 1.0 1.0 1.0 1.0 1.0 640.0 1.0 1.0 ... 99.0 1.0 96.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
AGO 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 350.0 1.0
ATG 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 5.0 ... 2626.0 711.0 1.0 1.0 1.0 39.0 1.0 1.0 1.0 1.0
ARG 9.0 66.0 104.0 1.0 1.0 9.0 1.0 1.0 561.0 1.0 ... 5135.0 1.0 132749.0 1.0 8.0 1240.0 5.0 1.0 1.0 2.0
ARM 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1061.0 1.0 1.0 1.0 1.0 1.0 1.0
ABW 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 612.0 1.0 1.0 1.0 1.0 3399.0 1.0 1.0 1.0 1.0
AUS 37482.0 3334.0 1561.0 380.0 0.0 580.0 11.0 15423.0 1371.0 42.0 ... 110643.0 1.0 11140.0 1625.0 1572.0 4895.0 227298.0 675.0 7121.0 38843.0
AUT 8620.0 3147.0 1290.0 1.0 2.0 341.0 7.0 1523.0 3051.0 1.0 ... 11054.0 1.0 282.0 754.0 7.0 915.0 3344.0 95.0 113.0 298.0
AZE 186.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 148503.0 1.0 ... 1.0 1.0 1.0 17030.0 1.0 1.0 1.0 1.0 1.0 1.0
BHS 1.0 1.0 1.0 1.0 1.0 1.0 14.0 111.0 1.0 1.0 ... 9664.0 1.0 17.0 1.0 1.0 30.0 1.0 1.0 1.0 15.0
BHR 656.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1120.0 1.0 1.0 1.0 1.0 1.0 1.0 15003.0 1.0 1.0
BGD 1.0 1.0 1.0 1.0 1.0 1.0 21664.0 1.0 1.0 1.0 ... 46008.0 1.0 1.0 1.0 1.0 1.0 27521.0 1.0 1.0 1.0
BRB 1.0 1.0 1.0 1.0 1.0 1.0 194.0 1.0 1.0 1.0 ... 1034.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BLR 282.0 1.0 53.0 1.0 1.0 1.0 1.0 102.0 5728.0 1.0 ... 87.0 1.0 28.0 14788.0 1.0 1.0 366.0 33.0 1.0 1.0
BEL 10638.0 7517.0 12054.0 1.0 1.0 4047.0 1.0 550.0 10433.0 1.0 ... 12853.0 1.0 115.0 1521.0 1.0 570.0 1498.0 140.0 123.0 149.0
BLZ 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 4036.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BEN 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BMU 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 3527.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BTN 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 136.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BOL 1.0 1.0 3.0 1.0 0.0 6.0 0.0 42691.0 5.0 2.0 ... 4896.0 1.0 509.0 5.0 0.0 773.0 12.0 0.0 2.0 3.0
BIH 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BWA 1.0 1.0 1.0 1.0 1.0 3550.0 1.0 1.0 1.0 1.0 ... 1277.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 12833.0 31625.0
BRA 69.0 1.0 219.0 1.0 12.0 7694.0 1.0 34699.0 63.0 1.0 ... 28068.0 1.0 28708.0 31.0 1.0 3425.0 152.0 1.0 1.0 122.0
VGB 1.0 1.0 1.0 1.0 1.0 1.0 752.0 1.0 1.0 1.0 ... 1306.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BRN 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 153.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
BGR 322.0 1134.0 369.0 1.0 0.0 7.0 5.0 119.0 1496.0 5.0 ... 2128.0 1.0 16.0 754.0 16.0 33.0 627.0 50.0 11.0 29.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
SWE 29085.0 2161.0 3004.0 1.0 1.0 758.0 1.0 3184.0 2520.0 1.0 ... 20011.0 1.0 2221.0 3555.0 1.0 1136.0 17004.0 1363.0 526.0 553.0
CHE 4613.0 2008.0 9600.0 1.0 54.0 6716.0 113.0 8863.0 741.0 1.0 ... 38347.0 1.0 1706.0 741.0 1.0 3989.0 12051.0 463.0 366.0 1074.0
SYR 1791.0 1.0 1.0 1.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0 1.0 1.0 1.0
TUV 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 17.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
UGA 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 994.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
UKR 2112.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 47307.0 1.0 ... 2738.0 1.0 1.0 219814.0 1.0 1.0 12390.0 1.0 1.0 1.0
ARE 7590.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 12953.0 1.0 1.0 1.0 1.0 1.0 1.0 173480.0 1.0 1.0
GBR 68256.0 14688.0 26826.0 1.0 63.0 15712.0 3995.0 11339.0 1790.0 678.0 ... 212150.0 134.0 1472.0 2864.0 168.0 9834.0 32429.0 19405.0 30897.0 132942.0
TZA 1.0 1.0 1.0 1.0 1.0 1249.0 1.0 1.0 1.0 1.0 ... 1291.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 8573.0 852.0
VIR 1.0 1.0 1.0 1.0 1.0 1.0 4587.0 1.0 1.0 501.0 ... 16861.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
USA 63255.0 81196.0 17700.0 1.0 1.0 12683.0 28558.0 179639.0 84953.0 5943.0 ... 1.0 1.0 47664.0 36743.0 1.0 197171.0 1302870.0 40861.0 22409.0 50001.0
URY 1.0 1.0 1.0 1.0 1.0 1.0 1.0 22521.0 1.0 1.0 ... 2853.0 1.0 1.0 1.0 1.0 463.0 1.0 1.0 1.0 1.0
UZB 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 5870.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
VUT 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
VEN 1.0 19.0 95.0 1.0 1.0 188.0 1.0 9944.0 34.0 486.0 ... 11242.0 1.0 4795.0 1.0 1.0 1.0 12.0 1.0 1.0 15.0
VNM 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 3164.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
YEM 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 15.0 1.0 1.0 1.0
ZMB 1.0 1.0 1.0 1.0 1.0 17464.0 1.0 1.0 1.0 1.0 ... 1360.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 19503.0
ZWE 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 30662.0 1.0

208 rows × 208 columns


In [6]:
visualization.matrix_plot(world.matrices["un"].iloc[:25, :25])


Warning: This function is going to (probably) take a long time.
Out[6]:
<module 'matplotlib.pyplot' from '/home/carl/.local/lib/python3.7/site-packages/matplotlib/pyplot.py'>

In [7]:
visualization.map_plot(world.data["Population"])


Out[7]:
<module 'matplotlib.pyplot' from '/home/carl/.local/lib/python3.7/site-packages/matplotlib/pyplot.py'>

In [8]:
visualization.map_plot(
    world.data["Population"] - world.data["Population"]["USA"],
    title="Population compared to U.S."
)


Out[8]:
<module 'matplotlib.pyplot' from '/home/carl/.local/lib/python3.7/site-packages/matplotlib/pyplot.py'>