# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Land area by number of involved conflicts as a function of Country

# Group Members: <NAME>, <NAME>, <NAME>, <NAME>, and Ryan (Hsin-Yuan) Wang

# Group Name: Allied Against An Anonymous Axis (aka 5A)

# Github Repo: https://github.com/jenna-jordan/IS590DV-FinalProject

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.cm as cm
from matplotlib.colors import Normalize
from matplotlib import gridspec

cshapes = pd.read_csv("../Data/CShapes/country_shapes.csv")

ucdp = pd.read_csv("../Data/UCDP-PRIO_ArmedConflict/participants_gw.csv")

# We start by grouping the dataset by the gw_id which is a number used to identify a country (Gleditsch & Ward number). from there we count the number of unique conflict id's which are assigned to each unique conflict. From there the countries are sorted by the counts is ascending order.

sorted_conflict_counts = ucdp.groupby("gw_id")[["conflict_id"]].count().sort_values(by=['conflict_id'])

# We then truncate the data into the top 20 and bottom 20 and sort each truncation is descending order, then rename the column to reflect that it is infact a count of conflicts and not a conflict id

top = sorted_conflict_counts.tail(20).sort_values(by=['conflict_id'], ascending=False)
bottom = sorted_conflict_counts.head(20).sort_values(by=['conflict_id'], ascending=False)
top.rename(columns={"conflict_id":"number_of_conflicts"},inplace=True)
bottom.rename(columns={"conflict_id":"number_of_conflicts"},inplace=True)
print(top)
print(bottom)

for key,item in top.iterrows():
    print(cshapes[cshapes["gw_code"] == key][["country_name","area"]])

for key,item in bottom.iterrows():
    print(cshapes[cshapes["gw_code"] == key][["country_name","area"]])

# It is then time to merge the conflict counts with teh cshapes dataset to get the associated area all in one table.

top_full = top.merge(cshapes, left_on="gw_id", right_on="gw_code")
bottom_full = bottom.merge(cshapes, left_on="gw_id", right_on="gw_code")
top_full

# +
my_cmap = cm.get_cmap('brg')
my_cmap2 = cm.get_cmap('hot')
my_norm = Normalize(vmin=40, vmax=300)
my_norm2 = Normalize(vmin=0, vmax=5)

fig2 = plt.figure(figsize=(30,30))
gs = gridspec.GridSpec(2, 2, width_ratios=[20, 1]) 
ax1 = plt.subplot(gs[0])
ax1.bar(top_full['country_name'],top_full['area'], color=my_cmap(my_norm(top_full['number_of_conflicts'].to_list())))
ax1.ticklabel_format(axis="y", style="plain")
ax1.set_title("Top 20 countries with the most conflicts", fontsize=30)
ax1.set_xlabel('Country Name', fontsize=20, labelpad=-50, fontweight='bold')
ax1.set_ylim([0,25000000])


ax2 = plt.subplot(gs[1])
cb1 = matplotlib.colorbar.ColorbarBase(ax2, cmap=my_cmap, norm=my_norm, orientation='vertical')
cb1.set_label('Number of Conflicts', fontsize=20, fontweight='bold', labelpad=20)

ax3 = plt.subplot(gs[2])
ax3.bar(bottom_full['country_name'],bottom_full['area'], color=my_cmap2(my_norm2(bottom_full['number_of_conflicts'].to_list())))
ax3.set_title("Top 20 countries with the least conflicts", fontsize=30)
ax3.ticklabel_format(axis="y", style="plain")
ax3.set_xlabel('Country Name', fontsize=20, labelpad=-50, fontweight='bold')
ax3.set_ylim([0,2500000])

ax4 = plt.subplot(gs[3])
cb2 = matplotlib.colorbar.ColorbarBase(ax4, cmap=my_cmap2, norm=my_norm2, orientation='vertical')
cb2.set_label('Number of Conflicts', fontsize=20, fontweight='bold', labelpad=20)

ax3.set_xlabel('Country Name', fontsize=20)
for ax in (ax1,ax3):
    ax.set_ylabel('Land Area ($km^2$)', fontsize=20, fontweight='bold', labelpad=20)
    ax.xaxis.set_tick_params(labelsize=20)
    ax.yaxis.set_tick_params(labelsize=20)
    
for ax_cmap in (ax2,ax4):
    ax_cmap.xaxis.set_tick_params(labelsize=20)
    ax_cmap.yaxis.set_tick_params(labelsize=20)

for ax in fig2.axes:
    matplotlib.pyplot.sca(ax)
    plt.xticks(rotation=70)

    
plt.suptitle("Land area of countries with most and least conflicts", fontsize=30, fontweight='bold')
plt.subplots_adjust(hspace=200)
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.savefig(fname="LandArea-Conflicts-Top_Bottom.png")
# -

# The first important thing to note about these graphs is that the y-axis scale in the top20 chart is actually an order of magnitude greater than the bottom20 chart (max of 25,000,000 instead of 2,500,000). Setting the plots to the same scale ended up making the bottom 20 bars almost hidden due to the drastic difference in scale. As such, a limit was placed on the y axis in order to make sure the bars can for the most part show up. This does show that there is a correlation to land area and number of conflicts. The larger a country, the better the chance that they have been involved in a large amount of conflicts.

# This then got us interested in what the middle portion looked like, so it was decided to plot the entire series of sorted number of conflicts and see if the expected trend held throughout the whole dataset. All 203 rows.

sorted_counts_full = sorted_conflict_counts.merge(cshapes, left_on="gw_id", right_on="gw_code")

sorted_counts_full.rename(columns={"conflict_id":"number_of_conflicts"},inplace=True)

sorted_counts_full

# +
my_cmap3 = cm.get_cmap('brg')
my_norm3 = Normalize(vmin=0, vmax=300)


fig3 = plt.figure(figsize=(50,30))
gs2 = gridspec.GridSpec(1, 2, width_ratios=[20, 1]) 
nax1 = plt.subplot(gs2[0])
nax1.bar(sorted_counts_full['gw_code'].astype(str),sorted_counts_full['area'], color=my_cmap3(my_norm3(sorted_counts_full['number_of_conflicts'].to_list())))
nax1.ticklabel_format(axis="y", style="plain")
#nax1.set_xlabel('Country Name', fontsize=20)
nax1.set_xticks([])

nax2 = plt.subplot(gs2[1])
ncb = matplotlib.colorbar.ColorbarBase(nax2, cmap=my_cmap3, norm=my_norm3, orientation='vertical')
ncb.set_label('Number of Conflicts', fontsize=20)

nax1.set_ylabel('Land Area ($km^2$)', fontsize=20)
nax1.xaxis.set_tick_params(labelsize=20)
nax1.yaxis.set_tick_params(labelsize=20)
    
nax2.xaxis.set_tick_params(labelsize=20)
nax2.yaxis.set_tick_params(labelsize=20)

    
plt.suptitle("Land area of countries compared to number of conflicts", fontsize=30)

plt.tight_layout(rect=[0, 0.3, 1, 0.95])

plt.savefig(fname="LandArea-Conflicts-Full.png")
# -

# The x-axis is intentionally left unlabled due to the inability to display any tick labels due to the closeness of the datapoints. This graph is only intended to show the trend of number of conflicts to land area across the whole dataset. The countries represented by the bars are sorted in order of number of total conflicts which is represented by the colormap as well. This confirmed that the predicted trend of larger countries having been involved in conflicts that was predicted above does hold relatively true throughout the dataset.

# +
#top_full.to_csv("top_full.csv")
#bottom_full.to_csv("bottom_full.csv")
#sorted_counts_full.to_csv("sorted_counts_full.csv")
# -



StarcoderdataJupyterScriptsDedupFiltered