<jupyter_start><jupyter_text>plot MI decay for additional units<jupyter_code>import pandas as pd
from parallelspaper.config.paths import DATA_DIR, FIGURE_DIR
from parallelspaper.speech_datasets import LCOL_DICT
import numpy as np
from parallelspaper import model_fitting as mf
from parallelspaper.utils import save_fig
LCOL_DICT
from matplotlib import gridspec
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>load data<jupyter_code>MI_DF = pd.read_pickle((DATA_DIR / 'MI_DF/language/language_MI_DF_fitted.pickle'))<jupyter_output><empty_output><jupyter_text>Plot additional units<jupyter_code>atd = [
        ['english', 'word', 'session'],
        ['english', 'pos', 'session'],
        ['german', 'word', 'session'],
        ['german', 'sylls', 'session'],
        ['japanese', 'pos', 'session'],
        ['japanese', 'word', 'session'],
        ['japanese', 'mora', 'session'],
    ]
subset_MI_DF = pd.concat(
    [MI_DF[
        (MI_DF.language.values == lang) &
        (MI_DF.unit.values == unit) &
        (MI_DF.analysis.values == analysis)
    ] for lang, unit, analysis in atd]
)
fontsize = 18
yoff=-.20
ncol = 4
nrow = int(np.ceil(len(subset_MI_DF)/ncol))
zoom = 5
fig, axs = plt.subplots(ncols=ncol, nrows=nrow, figsize=zoom*np.array([ncol,nrow]))
plt.subplots_adjust(hspace=0.25, wspace=0.25)

for axi, (idx, row) in enumerate(subset_MI_DF.sort_values(by=['language', 'unit']).iterrows()):
    ax = axs.flatten()[axi]
    
    color = LCOL_DICT[row.language]
    sig = np.array(row.MI-row.MI_shuff)
    distances = row.distances
    sig = sig
    distances = distances
    # get signal limits
    sig_lims = np.log([np.min(sig[sig>0]), np.nanmax(sig)])
    sig_lims = [sig_lims[0] - (sig_lims[1]-sig_lims[0])/10,
                    sig_lims[1] + (sig_lims[1]-sig_lims[0])/10]
            
    
    
    # plot real data
    ax.scatter(distances, sig, alpha = 1, s=40, color=color)
    
    best_fit_model = np.array(['exp','pow','pow_exp'])[np.argmin(row[['AICc_exp', 'AICc_power', 'AICc_concat']].values)]
    
    # set title
    model_type = {'pow_exp': 'comp.', 'exp': 'exp.', 'pow':'power law'}[best_fit_model]
    unit = {'pos': 'parts of speech', 'word': 'words', 'sylls': 'syllables', 'mora': 'mora'}[row.unit]
    
    ax.set_title(' | '.join([row.language.capitalize(), unit, model_type]), fontsize=16)
    
    if axi%ncol == 0:
            ax.set_ylabel('Mutual Information (bits)', labelpad=5, fontsize=fontsize)
            ax.yaxis.set_label_coords(yoff,0.5)
    
    ax.set_xlabel('Distance ('+unit+')', labelpad=5, fontsize=fontsize)
    ax.set_xticks([1,10,100])
    ax.set_xticklabels(['1','10','100'])
    
    # plot model
    distances_model = np.logspace(0,np.log10(distances[-1]), base=10, num=1000)
    
    if best_fit_model == 'pow_exp':
        print(row.language, row.unit, distances_model[int(row.min_peak)])
        ax.axvline(distances_model[int(row.min_peak)], lw=3,alpha=0.5, color=color, ls='dashed')
        
    if best_fit_model == 'pow_exp':
        # model data
        #row.concat_results.params.intercept = 0
        y_model = mf.get_y(mf.pow_exp_decay, row.concat_results, distances_model)
        y_pow = mf.get_y(mf.powerlaw_decay, row.concat_results, distances_model)
        y_exp = mf.get_y(mf.exp_decay, row.concat_results, distances_model)

        ax.plot(distances_model, y_pow, ls='dotted', color= 'k', lw=5, alpha=0.5)
        ax.plot(distances_model, y_exp-row.concat_results.params['intercept'].value, ls='dashed', color= 'k', lw=5, alpha=0.5)

        # plot modelled data
        ax.plot(distances_model, y_model, alpha = 0.5, lw=10, color=color)
    
    elif best_fit_model == 'pow':
        y_model = mf.get_y(mf.powerlaw_decay, row.pow_results, distances_model)
        # plot modelled data
        ax.plot(distances_model, y_model, alpha = 0.5, lw=10, color=color)
        
        
    elif best_fit_model == 'exp':
        y_model = mf.get_y(mf.exp_decay, row.exp_results, distances_model)
        # plot modelled data
        ax.plot(distances_model, y_model, alpha = 0.5, lw=10, color=color)
        
    # axis params
    ax.set_xlim([distances[0], distances[-1]])
    #sig_lims[0] = np.log(10e-6)
    
    ax.tick_params(which='both', direction='in', labelsize=14, pad=10)
    ax.tick_params(which='major', length=10, width =3)
    ax.tick_params(which='minor', length=5, width =2)
    ax.set_xscale( "log" , basex=10)
    ax.set_yscale( "log" , basey=10)
    ax.set_xticks([])
    for axis in ['top','bottom','left','right']:
        ax.spines[axis].set_linewidth(3)
        ax.spines[axis].set_color('k')
    
    ax.set_xlim([1,100])
    ax.set_ylim(np.exp(sig_lims))
    
    ax.set_xticks([1,10,100])
    ax.set_xticklabels(['1','10','100'])
    
for axi in np.arange(axi+1,ncol*nrow):
    ax = axs.flatten()[axi]
    ax.axis('off')

plt.subplots_adjust(hspace = 0.35)
#plt.tight_layout()
save_fig(FIGURE_DIR/'lang_fig_units')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16172344689378756, 'fraction_numerical': 0.02344689378757515, 'mean_word_length': 3.566331198536139, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '152125', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1909, 'n_words': 319, 'path': 'notebooks/language/09.0-language-plot-MI-all-units.ipynb', 'repo_name': 'timsainb/ParallelsBirdsongLanguagePaper', 'size': '4990'}