# plot the small simulation check (data generated by small_simulation_check.R)

import pandas as pd
import matplotlib.pyplot as plt

# parameters
# ---

# where are the small-simulation results
fname_sim = '../../results/simuln_check/small_simulation_check.csv'

# where are the analytic results
fname_ana = '../../results/members_recruit/isocline_7.csv'          # the analytic isoclines
fname_cri = '../../results/members_recruit/isocline_critpts.csv'    # critical points

# where to send results
dir_out = '../../results/simuln_check/'


# read in both simulation and analytic results
# ---

df_sim = pd.read_csv(fname_sim)
df_ana = pd.read_csv(fname_ana)
df_cri = pd.read_csv(fname_cri)


# extract the relevant critical points
# ---

q_0 = df_cri[df_cri['suffix']=='_7'].iloc[0]['q_0']
q_1 = df_cri[df_cri['suffix']=='_7'].iloc[0]['q_1']
h_0 = 1-q_0
h_1 = 1-q_1


# plot
# ---

plt.figure(figsize=(5, 3.8))

# plot analytic results
plt.plot(1-df_ana['q'], df_ana['p_stable'], color='black', ls='solid', lw=3, zorder=1, label='analytic stable')
plt.plot(1-df_ana['q'], df_ana['p_unstable'], color='black', ls='dashed', zorder=1, label='analytic unstable')
plt.plot([0, h_0], [0, 0],  color='black', zorder=1, lw=3)
plt.plot([0, h_1], [1, 1],  color='black', zorder=1, ls='dashed')

# plot simulation results
for col_name in df_sim.columns[1:-1]:
    plt.scatter(df_sim['h'], df_sim[col_name], zorder=2, alpha=0.7, color='blue')
plt.scatter(df_sim['h'], df_sim[df_sim.columns[-1]], zorder=2, alpha=0.7, color='blue', label='simulation endpoints')

# decorate
plt.xlabel(r'homophily $h$', fontsize='x-large')
plt.ylabel(r'proportion of Cooperators $p$', fontsize='x-large')
plt.legend(loc='best')

plt.tight_layout()
plt.savefig(dir_out + 'small_simulation_check.pdf')
plt.close()
