import pandas as pd
import matplotlib as mpl
import seaborn as sb
import itertools
%matplotlib inline

by_destination = pd.read_csv("../data/WRAPS-arrivals-by-destination-2005-2015-clean.csv")
by_religion = pd.read_csv("../data/WRAPS-arrivals-by-religion-2005-2015-clean.csv")

# The totals for the two data-reports are off by just four arrivals.
print(by_destination["arrivals"].sum())
print(by_religion["arrivals"].sum())

state_populations = pd.read_csv("../data/census-state-populations.csv").set_index("state")

colors = itertools.cycle([ "#3498db","#2ecc71", "#9b59b6" ])

"""
Explanation: Analysis of U.S. Refugee Data
The refugee data in this notebook comes from the Department of State's Refugee Processing Center's data portal. It was retrieved on November 18, 2015.
Load the data
End of explanation
"""


def arrivals_by_year(origin=None, dest_state=None, dest_city=None):
    return by_destination[
        (by_destination["origin"] == (origin if origin else by_destination["origin"])) &
        (by_destination["dest_state"] == (dest_state if dest_state else by_destination["dest_state"])) &
        (by_destination["dest_city"] == (dest_city if dest_city else by_destination["dest_city"]))
    ].groupby("year")["arrivals"].sum()

def make_vbar(series, title):
    ax = series.plot(kind="bar", width=0.85,
        figsize=(10, 6),
        fontsize=14,
        color=next(colors))
    ax.figure.set_facecolor("white")
    ax.set_ylim(0, series.max() * 1.05)
    ax.set_yticklabels([ "{0:,.0f}".format(y) for y in ax.get_yticks() ])
    mpl.pyplot.setp(ax.get_children()[len(series) + 1], alpha=0.5)
    mpl.pyplot.setp(ax.get_xticklabels(), rotation=0)
    ax.set_xlabel("")
    ax.set_title(title, fontsize=16, fontweight="bold", alpha=0.75)
    return ax

def make_hbar(series, title, width=9, height=20):
    ax = series.plot(kind="barh", width=0.75,
        figsize=(width, height),
        fontsize=14,
        color=next(colors))
    ax.figure.set_facecolor("white")
    ax.set_xlim(0, series.max() * 1.05)
    ax.set_xticklabels([ "{0:,.0f}".format(x) for x in ax.get_xticks() ])
    ax.invert_yaxis()
    ax.set_ylabel("")
    ax.set_title(title, fontsize=16, fontweight="bold", alpha=0.75)
    return ax

"""
Explanation: Set up a few data-processing and chart-making functions
End of explanation
"""


ax = make_vbar(arrivals_by_year(), "Refugee Arrivals in U.S., 2005 – 2015")
pass

ax = make_vbar(arrivals_by_year(origin="Syria"), "Syrian Refugee Arrivals in U.S., 2005 – 2015")
pass

"""
Explanation: Refugee arrivals by year
End of explanation
"""


origin_counts = by_destination.groupby("origin")["arrivals"].sum()
ax = make_hbar(origin_counts.sort_values(ascending=False).head(19),
    "Top Origins For U.S. Refugee Arrivals, 2005 – 2015",
    height=(20 * 19 / 51))
pass

"""
Explanation: Refugee arrivals by origin
End of explanation
"""


by_religion.groupby("religion")["arrivals"].sum().sort_values(ascending=False)

"""
Explanation: Refugee arrivals by religion
End of explanation
"""


muslim_arrivals = by_religion[
    by_religion["religion"].str.contains("Moslem")
]

muslim_arrivals_by_year = muslim_arrivals.groupby("year")["arrivals"].sum()
muslim_arrivals_by_year

print("Since 2005, about {0:.0f}% of arriving refugees have been Muslim.".format(
    muslim_arrivals["arrivals"].sum() * 100.0 / by_religion["arrivals"].sum())
)

muslim_ratio = muslim_arrivals_by_year / by_religion.groupby("year")["arrivals"].sum()
ax = make_vbar(muslim_ratio, "Muslim Refugees, as Pct. of All U.S. Refugee Arrivals")
ax.set_yticklabels([ "{0:.0f}%".format(y * 100) for y in ax.get_yticks() ])
ax.set_ylim(0, 0.5)
pass

muslim_origins = muslim_arrivals.groupby("origin")["arrivals"].sum()

ax = make_hbar(muslim_origins.sort_values(ascending=False).head(10),
    "Muslim Refugees Arrivals By Origin, 2005 – 2015", height=10*20/51)

"""
Explanation: Note: The code below groups four separate designations — "Moslem," "Moslem Suni," "Moslem Shiite," and "Moslem Ismaici" (likely a typo for "Moslem Ismaili") — into a single "Muslim" grouping.
End of explanation
"""


large_single_populations = by_destination.groupby([ "origin", "dest_state", "dest_city" ])["arrivals"].sum()\
    .sort_values(ascending=False).head(15)

ax = make_hbar(large_single_populations, "Refugee Country → Destination Flows, 2005 – 2015", height=15*20/51)
ax.set_yticklabels([ "{0} → {2}, {1}".format(*y) for y in large_single_populations.index ])
pass

"""
Explanation: Refugees by U.S. destination
End of explanation
"""


all_by_state = pd.DataFrame(state_populations["pop_est_2014"]).join(pd.DataFrame({
    "arrivals": by_destination.groupby("dest_state")["arrivals"].sum(),
})).fillna(0)
all_by_state["per_1k_residents"] = all_by_state["arrivals"] * 1000.0 / all_by_state["pop_est_2014"]
all_by_state.sort_values("per_1k_residents", ascending=False)

ax = make_hbar(all_by_state["per_1k_residents"].sort_values(ascending=False),
    "Refugee Arrivals Per 1,000 Residents, 2005 – 2015")
pass

"""
Explanation: Note: State population data comes from the Census Bureau's 2014 estimates.
End of explanation
"""


