Datasets:
Upload Data/__init__.py with huggingface_hub
Browse files- Data/__init__.py +68 -0
Data/__init__.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# We make relevant datasets contained in this folder available across the project, for easier integration and plotting.
|
| 5 |
+
# The base directory points to the folder containing this file:
|
| 6 |
+
base_dir = os.path.dirname(__file__)
|
| 7 |
+
|
| 8 |
+
# Make the ISO code lookup table available across the project. Also group countries by region.
|
| 9 |
+
lookup_table = pd.read_csv(os.path.join(base_dir, "Iso_code_lookup.csv"))
|
| 10 |
+
countries_by_region = pd.read_csv(os.path.join(base_dir, "countries_by_region.csv"))
|
| 11 |
+
countries_by_region = countries_by_region.groupby('Region').agg(list).to_dict()['Country ISO']
|
| 12 |
+
countries_by_region['Eastern Asia'].append('TWN')
|
| 13 |
+
countries_by_region['Middle East'] = countries_by_region.pop('Western Asia')
|
| 14 |
+
countries_by_region['South Asia'] = countries_by_region.pop('Southern Asia')
|
| 15 |
+
|
| 16 |
+
# Make the world shapefile available for plotting
|
| 17 |
+
import geopandas as gpd
|
| 18 |
+
world = gpd.read_file(os.path.join(base_dir, "world_shapefile/ne_50m_admin_0_countries.shp"))
|
| 19 |
+
world.to_crs("EPSG:3857", inplace=True) # Mercator projection by default
|
| 20 |
+
|
| 21 |
+
# Make the coordinates available. These are regular latitude/longitude coordinates
|
| 22 |
+
coordinates = pd.read_csv(os.path.join(base_dir, "Coordinates.csv"))[['Alpha-3 code', 'Latitude', 'Longitude']].set_index('Alpha-3 code').to_dict()
|
| 23 |
+
coordinates = dict((k, (coordinates['Latitude'][k], coordinates['Longitude'][k])) for k in coordinates['Latitude'].keys())
|
| 24 |
+
|
| 25 |
+
# Load the various comparison datasets into a dictionary for easy plotting, assigning consistent colours
|
| 26 |
+
# and markers for each.
|
| 27 |
+
from Plots.colors import colors
|
| 28 |
+
import xarray as xr
|
| 29 |
+
flow_dsets = {
|
| 30 |
+
"Quantmig": dict(
|
| 31 |
+
data=xr.open_dataset(os.path.join(base_dir, "Flow_data/QuantMig_data/Quantmig_flows.nc")),
|
| 32 |
+
central='flow_50%', lower='flow_2.5%', upper='flow_97.5%',
|
| 33 |
+
primary_color=colors['c_orange'], secondary_color=colors['c_yellow']
|
| 34 |
+
),
|
| 35 |
+
"Statistics Sweden": dict(
|
| 36 |
+
data=xr.open_dataarray(os.path.join(base_dir, "Flow_data/National_Statistics/SWE_flows.nc")),
|
| 37 |
+
primary_color=colors['c_red'], secondary_color=colors['c_pink'], marker='v', s=10,
|
| 38 |
+
),
|
| 39 |
+
"Statistics Finland": dict(
|
| 40 |
+
data=xr.open_dataarray(os.path.join(base_dir, "Flow_data/National_Statistics/FIN_flows.nc")),
|
| 41 |
+
primary_color=colors['c_lightblue'], secondary_color=colors['c_darkblue'], marker='s', s=10
|
| 42 |
+
),
|
| 43 |
+
"StatNZ": dict(
|
| 44 |
+
data=xr.open_dataarray(os.path.join(base_dir, "Flow_data/National_Statistics/NZL_flows.nc")),
|
| 45 |
+
primary_color=colors['c_darkgreen'], secondary_color=colors['c_lightgreen'], marker='^', s=10
|
| 46 |
+
),
|
| 47 |
+
"Facebook data": dict(
|
| 48 |
+
data=xr.open_dataarray(os.path.join(base_dir, "Flow_data/Facebook/facebook_flows.nc")).where(lambda x: x>=25),
|
| 49 |
+
primary_color=colors['c_purple'], secondary_color='#E1C6EC', ec=colors['c_darkgrey'], marker='o', s=10, lw=0.5
|
| 50 |
+
)
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# UN WPP data
|
| 54 |
+
population = 1e3 * xr.open_dataset(os.path.join(base_dir, "UN_WPP_data/UN_WPP_data.nc"))['Total Population, as of 1 January (thousands)']
|
| 55 |
+
death_rate = 1e-3 * xr.open_dataset(os.path.join(base_dir, "UN_WPP_data/UN_WPP_data.nc"))['Crude Death Rate (deaths per 1,000 population)']
|
| 56 |
+
WPP_net_migration = 1e3 * xr.open_dataset(os.path.join(base_dir, "UN_WPP_data/UN_WPP_data.nc"))['Net Number of Migrants (thousands)']
|
| 57 |
+
NatStat_net_migration = xr.open_dataarray(os.path.join(base_dir, "Net_migration/National_statistics.nc"))
|
| 58 |
+
WPP_data = xr.open_dataset(os.path.join(base_dir, "UN_WPP_data/UN_WPP_data.nc"))
|
| 59 |
+
|
| 60 |
+
# Calculate the fraction of population alive at start of 1990 in each destination country still alive at start of year
|
| 61 |
+
import numpy as np
|
| 62 |
+
gamma = (1-death_rate.sel({"Year": range(1989, 2024)})).assign_coords({"Year": np.arange(1990, 2025, 1)}).rename({
|
| 63 |
+
"Country ISO": "Destination ISO"})
|
| 64 |
+
gamma.loc[{"Year": 1990}] = 1.0
|
| 65 |
+
gamma = gamma.cumprod('Year')
|
| 66 |
+
|
| 67 |
+
# Stock data
|
| 68 |
+
stock_data = xr.load_dataset(os.path.join(base_dir, "UN_stock_data/stock_data.nc"))
|