<jupyter_start><jupyter_text>load distributions coverage k volte, con n0 e t0 random<jupyter_code>ORIGINAL_COLOR = '#474747' #dark grey
ETN_COLOR =  '#fb7041' #'#E5865E' # arancio
TAG_COLOR = '#96ccc8' # light blue
STM_COLOR = '#bad1f2' #8F2E27' # rosso
DYM_COLOR = '#559ca6' # teal



line_width = 1.5

def compute_counts(ro,e):
    counts = []
    e = np.array(e)
    for i in range(len(ro)-1):
        r1 = ro[i]
        r2 = ro[i+1]
        ee = e[e>r1]
        count = ee[ee<=r2]
        counts.append(len(count))
    return counts

def compute_multpile_counts(ranges,ee):
    counts = []
    for e in ee:
        counts.append(compute_counts(ranges,e))
    return counts



# example of calculating the kl divergence between two mass functions
from math import log2
o_in,stb_in,e_in,t_in = load_r0("High_School12",["la025","la015","la001"])


def compute_ks_r0(file_name,nb_bins=50):
    res_etn = []
    res_tag = []
    res_stab = []
    o_in,stb_in,e_in,t_in = load_r0(file_name,["la025","la015","la001"])
    
    
    for k in range(3):
        res_stab.append(compute_kl_divergence(o_in[k],stb_in[k],nb_bins))
        
    for k in range(3):
        tmp = []
        for i in [e_in[k]]:
            kl = compute_kl_divergence(o_in[k],i,nb_bins)
            tmp.append(kl)
        #res_etn.append([np.mean(tmp),np.std(tmp)])
        res_etn.append([np.mean(tmp),np.std(tmp)/np.sqrt(len(tmp))])
        



    for k in range(3):
        tmp = []
        for i in [t_in[k]]:
            kl = compute_kl_divergence(o_in[k],i,nb_bins)
            tmp.append(kl)
        #res_tag.append([np.mean(tmp),np.std(tmp)])
        res_tag.append([np.mean(tmp),np.std(tmp)/np.sqrt(len(tmp))])



    return [res_stab,np.array(res_etn),np.array(res_tag)]
load_mfpt("High_School12")
def load_cov(file_name):
    ori_cov = np.load("dynamic_results/SI/"+file_name+"/coverage/orig.npy")
    sta_cov = np.load("dynamic_results/SI/"+file_name+"/coverage/stab.npy")
    etn_cov = np.load("dynamic_results/SI/"+file_name+"/coverage/etn.npy")
    tag_cov = np.load("dynamic_results/SI/"+file_name+"/coverage/tag.npy")
    
    return ori_cov,sta_cov,etn_cov,tag_cov

def load_mfpt(file_name):
    ori_cov = np.load("dynamic_results/SI/"+file_name+"/MFPT/orig.npy")
    sta_cov = np.load("dynamic_results/SI/"+file_name+"/MFPT/stab.npy")
    etn_cov = np.load("dynamic_results/SI/"+file_name+"/MFPT/etn.npy")
    tag_cov = np.load("dynamic_results/SI/"+file_name+"/MFPT/tag.npy")
    
    return ori_cov,sta_cov,etn_cov,tag_cov


def compute_cov_give_file(file_name,nb_bins=50):

    ori_cov,sta_cov,etn_cov,tag_cov = load_cov(file_name)

    ec_kl = []
    tc_kl = []
    
    for i in [etn_cov]:
        ec_kl.append(compute_kl_divergence(ori_cov,i,n_bins=nb_bins))
    for i in [tag_cov]:
        tc_kl.append(compute_kl_divergence(ori_cov,i,n_bins=nb_bins))

    stb = compute_kl_divergence(ori_cov,sta_cov,n_bins=nb_bins)

    #res = [[stb],
    #       [np.nanmean(ec_kl),np.nanstd(ec_kl)],[np.nanmean(tc_kl),np.nanstd(tc_kl)],
    #       [np.nanmean(sc_kl),np.nanstd(sc_kl)],[np.nanmean(dc_kl),np.nanstd(dc_kl)]]

    res = [[stb],[np.nanmean(ec_kl),np.nanstd(ec_kl)/np.sqrt(len(ec_kl))],[np.nanmean(tc_kl),np.nanstd(tc_kl)/np.sqrt(len(tc_kl))]]
    
    return res

def compute_mfpt_give_file(file_name,nb_bins=50):
    
    ori_cov,sta_cov,etn_cov,tag_cov = load_mfpt(file_name)
    
    ec_kl = []
    tc_kl = []
    
    for i in [etn_cov]:
        ec_kl.append(compute_kl_divergence(ori_cov,i,n_bins=nb_bins))
    for i in [tag_cov]:
        tc_kl.append(compute_kl_divergence(ori_cov,i,n_bins=nb_bins))


    stb = compute_kl_divergence(ori_cov,sta_cov,n_bins=nb_bins)

    #res = [[stb],
    #       [np.nanmean(ec_kl),np.nanstd(ec_kl)],[np.nanmean(tc_kl),np.nanstd(tc_kl)],
    #       [np.nanmean(sc_kl),np.nanstd(sc_kl)],[np.nanmean(dc_kl),np.nanstd(dc_kl)]]

    res = [[stb],
           [np.nanmean(ec_kl),np.nanstd(ec_kl)/np.sqrt(len(ec_kl))],[np.nanmean(tc_kl),np.nanstd(tc_kl)/np.sqrt(len(tc_kl))]]
    
    return res
ori_cov,sta_cov,etn_cov,tag_cov = load_cov("High_School12")



def load_r0(file_name,lambds=["la001"]):
    ori = []
    stb = []
    etn = []
    tag = []
    for lambd in lambds:
        ori.append(np.load("dynamic_results/SI/"+file_name+"/R0/"+lambd+"/orig.npy"))
        stb.append(np.load("dynamic_results/SI/"+file_name+"/R0/"+lambd+"/stab.npy"))
        etn.append(np.load("dynamic_results/SI/"+file_name+"/R0/"+lambd+"/etn.npy"))
        tag.append(np.load("dynamic_results/SI/"+file_name+"/R0/"+lambd+"/tag.npy"))

    return ori,stb,etn,tag
    
import math



from matplotlib.patches import Patch
from matplotlib.lines import Line2D




def plot_cov_mfpt3(ax,file_name,nb_bins=50,legend=False):
    
    if file_name == "High_School12":
        #ax.set_title("Hospital")
        ax.set_title("High school 2")
    if file_name == "High_School13":
        ax.set_title("High school 3")
    if file_name == "primary_school":
        ax.set_title("Primary school")
        
    if file_name == "DTU_calls_1set":
        ax.set_title("Calls 1")
    if file_name == "FF_calls_1set":
        ax.set_title("Calls 2")
    if file_name == "DTU_SMS_1set":
        ax.set_title("SMS 1")
    if file_name == "FF_SMS_1set":
        ax.set_title("SMS 2")
        
    cov = compute_cov_give_file(file_name,nb_bins)
    mfpt = compute_mfpt_give_file(file_name,nb_bins)
    
    x = np.arange(2)
    
    x1 = np.array([cov[0],mfpt[0]])
    print(x1)
    x2 = np.array([cov[1],mfpt[1]])
    x3 = np.array([cov[2],mfpt[2]])
    


    width = 0.2
    rects1 = ax.bar(x - 0.1, x2[:,0], width,  label='ETN-gen',color=ETN_COLOR)
    rects5 = ax.bar(x + 0.1, x3[:,0], width,  label='TagGen',color=TAG_COLOR)
    
    ax.plot([-0.45,0.45],[x1[1],x1[1]],linewidth=3, label='Stability',color=ORIGINAL_COLOR)
    ax.plot([1-0.45,1.45],[x1[0],x1[0]],linewidth=3, label='Stability',color=ORIGINAL_COLOR)
    
              
    ax.tick_params(bottom=False, right=False,left=False)
    ax.set_axisbelow(True)
    
    ax.yaxis.grid(True, color='lightgrey')
    ax.xaxis.grid(False)
    
    #ax.yaxis.grid(True, color='#FFFFFF')
    #ax.set_facecolor('#EFEFEF')
    #ax.xaxis.grid(False)
    
    
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_color('#DDDDDD')    
    
    labels = ["Coverage","MFPT"]
    ax.set_xticks(x)
    ax.set_xticklabels(labels,rotation=0)
    #ax.set_ylim((0,1))

        
def empty_plot(ax):
    
    ax.plot()
    ax.yaxis.grid(False)
    ax.xaxis.grid(False)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    # No ticks
    ax.set_xticks([])
    ax.set_yticks([])
    

def plot_r03(ax,file_name,legend=False):

    if file_name == "High_School12":
        #ax.set_title("Hospital")
        ax.set_title("High school 2")
    if file_name == "High_School13":
        ax.set_title("High school 3")
    if file_name == "primary_school":
        ax.set_title("Primary school")
        
        
    if file_name == "DTU_calls_1set":
        ax.set_title("Calls 1")
    if file_name == "FF_calls_1set":
        ax.set_title("Calls 2")
    if file_name == "DTU_SMS_1set":
        ax.set_title("SMS 1")
    if file_name == "FF_SMS_1set":
        ax.set_title("SMS 2")
        
    x1,x2,x3 = compute_ks_r0(file_name)
    x = np.arange(3)
    width = 0.2
    
    error_bar_style = dict(ecolor=ORIGINAL_COLOR, alpha=0.8, lw=1.5, capsize=4, capthick=1)

    rects1 = ax.bar(x - 0.1, x2[:,0], width, label='ETN-gen',color=ETN_COLOR)
    rects5 = ax.bar(x + 0.1, x3[:,0], width, label='TagGen',color=TAG_COLOR)
    
    ax.plot([-0.45,0.45],[x1[0],x1[0]],linewidth=3, label='Stability',color=ORIGINAL_COLOR)
    ax.plot([1-0.45,1.45],[x1[1],x1[1]],linewidth=3, label='Stability',color=ORIGINAL_COLOR)
    ax.plot([2-0.45,2.45],[x1[2],x1[2]],linewidth=3, label='Stability',color=ORIGINAL_COLOR)
    
          
    ax.tick_params(bottom=False, right=False,left=False)
    ax.set_axisbelow(True)
    
    #ax.yaxis.grid(True, color='#FFFFFF')
    #ax.set_facecolor('#EFEFEF')
    #ax.xaxis.grid(False)
    
    ax.yaxis.grid(True, color='lightgrey')
    ax.xaxis.grid(False)
    
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_color('#DDDDDD')
    
    

    labels = [r"$\lambda$ = 0.25",r"$\lambda$ = 0.15",r"$\lambda$ = 0.01"]
    ax.set_xticks(x)
    ax.set_xticklabels(labels,rotation=0)
    #ax.set_ylim((0,1))
    
    if legend:
        c = 0
fig, ax = plt.subplots(1,7, figsize=(12,3), gridspec_kw={'width_ratios': [1,1,1,0.2,1.5,1.5,1.5]})
fig.tight_layout(w_pad=-1)

ax[0].get_shared_y_axes().join(ax[0], ax[1],ax[2])
ax[4].get_shared_y_axes().join(ax[4], ax[5],ax[6])
plot_cov_mfpt3(ax[0],"High_School12")
plot_cov_mfpt3(ax[1],"High_School13")
plot_cov_mfpt3(ax[2],"primary_school",legend=True)

empty_plot(ax[3])

plot_r03(ax[4],"High_School12")
plot_r03(ax[5],"High_School13")
plot_r03(ax[6],"primary_school",legend=True)


ax[1].tick_params(axis='y', colors='white')
ax[2].tick_params(axis='y', colors='white')

#ax[4].tick_params(axis='y', colors='white')
ax[5].tick_params(axis='y', colors='white')
ax[6].tick_params(axis='y', colors='white')

legend_elements = [Line2D([0], [0], color=ORIGINAL_COLOR, lw=3,label='Original'),
                   Patch(facecolor=ETN_COLOR, edgecolor=ETN_COLOR,label='ETN-Gen'),
                   Patch(facecolor=TAG_COLOR, edgecolor=TAG_COLOR,label='TagGen')]

# Create the figure
ax[0].legend(handles=legend_elements,loc='center left', bbox_to_anchor=(-0.2, -0.2),ncol=5)

fig.text(0.17, 1.08, 'Random walk',fontdict={'size':14,'color':'#4d4d4d'},weight="bold")
fig.text(0.682, 1.08, 'SIR model',fontdict={'size':14,'color':'#4d4d4d'},weight="bold")


fig.savefig("dynamic_main_test_kld_SI_face-to-face.pdf", bbox_inches = 'tight')<jupyter_output>[[0.01496661]
 [0.00369286]]
[[0.05499775]
 [0.00172908]]
[[0.08214685]
 [0.00210313]]<jupyter_text>SMS calls<jupyter_code>fig, ax = plt.subplots(1,9, figsize=(15,4), gridspec_kw={'width_ratios': [1,1,1,1,0.2,1.5,1.5,1.5,1.5]})
fig.tight_layout(w_pad=-1)

ax[0].get_shared_y_axes().join(ax[0], ax[1],ax[2],ax[3])
ax[5].get_shared_y_axes().join(ax[5], ax[6],ax[7],ax[8])
plot_cov_mfpt3(ax[0],"DTU_calls_1set")
plot_cov_mfpt3(ax[1],"FF_calls_1set")
plot_cov_mfpt3(ax[2],"DTU_SMS_1set")
plot_cov_mfpt3(ax[3],"FF_SMS_1set",legend=True)

empty_plot(ax[4])

plot_r03(ax[5],"DTU_calls_1set")
plot_r03(ax[6],"FF_calls_1set")
plot_r03(ax[7],"DTU_SMS_1set",legend=True)
plot_r03(ax[8],"FF_SMS_1set",legend=True)


ax[1].tick_params(axis='y', colors='white')
ax[2].tick_params(axis='y', colors='white')
ax[3].tick_params(axis='y', colors='white')

#ax[4].tick_params(axis='y', colors='white')
#ax[5].tick_params(axis='y', colors='white')
ax[6].tick_params(axis='y', colors='white')
ax[7].tick_params(axis='y', colors='white')
ax[8].tick_params(axis='y', colors='white')

legend_elements = [Line2D([0], [0], color=ORIGINAL_COLOR, lw=3,label='Original'),
                   Patch(facecolor=ETN_COLOR, edgecolor=ETN_COLOR,label='ETN-Gen'),
                   Patch(facecolor=TAG_COLOR, edgecolor=TAG_COLOR,label='TagGen')]

# Create the figure
ax[0].legend(handles=legend_elements,loc='center left', bbox_to_anchor=(-0.2, -0.2),ncol=5)

fig.text(0.17, 1.08, 'Random walk',fontdict={'size':14,'color':'#4d4d4d'},weight="bold")
fig.text(0.682, 1.08, 'SIR model',fontdict={'size':14,'color':'#4d4d4d'},weight="bold")


fig.savefig("dynamic_main_test_kld_SI_sms_calls.pdf", bbox_inches = 'tight')
def one_line_plot(ax,ori,stab,etn,stm,tag,dym):
    #ax[0].figure(figsize=(15,3))
    #ax[0].supset_title(set_title)
    ax[0].hist(ori)
    ax[0].set_title("orig")
    
    ax[1].hist(stab)
    ax[1].set_title("stab")
    
    ax[2].hist(etn[0][0])
    ax[2].set_title("etn")
    
    ax[3].hist(stm[0][0])
    ax[3].set_title("stm")
    
    ax[4].hist(tag[0][0])
    ax[4].set_title("tag")
    
    ax[5].hist(dym[0][0])
    ax[5].set_title("dym")
filename = "High_School11"
fig, ax = plt.subplots(3,6, figsize=(15,9))

fig.text(0.5, 0.95, filename,fontdict={'size':14,'color':'#4d4d4d'},weight="bold")
fig.text(0.08, 0.2, 'la 0025',fontdict={'size':14,'color':'#4d4d4d'},weight="bold",rotation="vertical")
fig.text(0.08, 0.5, 'la 0015',fontdict={'size':14,'color':'#4d4d4d'},weight="bold",rotation="vertical")
fig.text(0.08, 0.75, 'la 0001',fontdict={'size':14,'color':'#4d4d4d'},weight="bold",rotation="vertical")

ori,stab,etn,stm,tag,dym = load_r0(filename,lambds=["la001"])
one_line_plot(ax[0],ori,stab,etn,stm,tag,dym)
ori,stab,etn,stm,tag,dym = load_r0(filename,lambds=["la015"])
one_line_plot(ax[1],ori,stab,etn,stm,tag,dym)
ori,stab,etn,stm,tag,dym = load_r0(filename,lambds=["la025"])
one_line_plot(ax[2],ori,stab,etn,stm,tag,dym)

fig.savefig("plot_distributions/dinamic_r0_"+filename+".pdf", bbox_inches = 'tight')
def one_line_plot(ax,ori,stab,etn,stm,tag,dym):
    #ax[0].figure(figsize=(15,3))
    #ax[0].supset_title(set_title)
    ax[0].hist(ori)
    ax[0].set_title("orig")
    
    ax[1].hist(stab)
    ax[1].set_title("stab")
    
    ax[2].hist(etn[0][0])
    ax[2].set_title("etn")
    
    ax[3].hist(stm[0][0])
    ax[3].set_title("stm")
    
    ax[4].hist(tag[0][0])
    ax[4].set_title("tag")
    
    ax[5].hist(dym[0][0])
    ax[5].set_title("dym")
filename = "InVS13"
fig, ax = plt.subplots(2,6, figsize=(15,6))

fig.text(0.5, 0.95, filename,fontdict={'size':14,'color':'#4d4d4d'},weight="bold")
fig.text(0.08, 0.2, 'cov',fontdict={'size':14,'color':'#4d4d4d'},weight="bold",rotation="vertical")
fig.text(0.08, 0.7, 'mfpt',fontdict={'size':14,'color':'#4d4d4d'},weight="bold",rotation="vertical")

ori,stab,etn,stm,tag,dym = load_mfpt(filename)
one_line_plot2(ax[0],ori,stab,etn,stm,tag,dym)

ori,stab,etn,stm,tag,dym = load_cov(filename)
one_line_plot2(ax[1],ori,stab,etn,stm,tag,dym)

fig.savefig("plot_distributions/dinamic_random_walk_"+filename+".pdf", bbox_inches = 'tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19841997601749312, 'fraction_numerical': 0.04542568949707272, 'mean_word_length': 4.31807951987997, 'pattern_counts': {'":': 14, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '537722', 'license': 'MIT', 'n_tokens_mistral': 6507, 'n_tokens_neox': 6177, 'n_words': 635, 'path': 'Evaluation/Dynamic-Multiple run-kl-TEST2-SI.ipynb', 'repo_name': 'AntonioLonga/ETNgen', 'size': '14177'}