<jupyter_start><jupyter_text>Generate Human Experiment Figure for KDG paper submissionIt is the same experiment from 5/18/2021, but modified for KDG paper for 2022 ICML submission. Here we `removed SVM`, and made minor styling changes.Update -- we further changed the style since last update<jupyter_code># changing cwd
%cd ..<jupyter_output>c:\Users\jongm\Desktop\temp_workspace\JOVO\inductive-bias<jupyter_text>Load packages<jupyter_code>from src.inductive_bias import IB
ib = IB() #instantiate inductive bias package
ib.load_sampledData()<jupyter_output>[ c:\Users\jongm\Desktop\temp_workspace\JOVO\inductive-bias\clf/SampledData.pickle ] loaded<jupyter_text>Time and Date of the experiment<jupyter_code>print(ib.date)<jupyter_output>2022-02-15 02:22:51.591256<jupyter_text>Load dependencies<jupyter_code>import seaborn as sns
import pandas as pd
import numpy as np
import pickle
from tqdm.notebook import tqdm

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle

cmap = 'bwr'

import warnings
warnings.filterwarnings('ignore')

#image output
import os
import imageio<jupyter_output><empty_output><jupyter_text>Extract human coordinates<jupyter_code>ib.extract_human_coord()
ib.humanLoc[0][0].head()<jupyter_output><empty_output><jupyter_text>Retraining ML models and predicting exactly the same xy coordinates from human experiment<jupyter_code>ib.mask = ib.generate_mask(h=0.1)
ib.mask.shape
#generic global
uX = ib.mask
uX0, uX1 = uX[:,0], uX[:,1]
label = ['DN'] + ib.mtype[2:3] + ['Human']
dtype = ib.dtype[2:5:2]
fsize = 25

#retrain global
reps = 126
saved_clf = ib.clf #using already optimized hyper-parameter from previous models
N_sample = 100 #same number of samples that of human
h = 0.1
rng = 3

#train
TRAIN_NOW = False

#figure
SAVEFIG = True
# run only for the first time
if TRAIN_NOW:
    ib.get_sampledData(saved_clf=saved_clf, reps=reps, N_sample=N_sample, h=0.1, rng=3)<jupyter_output><empty_output><jupyter_text>The grid matching yields equal number of points between human and machine<jupyter_code># ML grid vs human grid
ib.estpst_sample[0][0].shape, ib.human[0].shape, ib.estpst_sample[1][0].shape, ib.human[1].shape<jupyter_output><empty_output><jupyter_text>The grid size matches after point-wise averaging<jupyter_code>ib.pointwise_gridAverage(ib.estpst_sample[0][0]).shape, ib.pointwise_gridAverage(np.column_stack([ib.human[0][:,3], ib.human[0][:,5], ib.human[0][:,0]])).shape
ib.pointwise_gridAverage(ib.estpst_sample[1][0]).shape, ib.pointwise_gridAverage(np.column_stack([ib.human[1][:,3], ib.human[1][:,5], ib.human[1][:,0]])).shape<jupyter_output><empty_output><jupyter_text>Point-wise averaging and gaussian smoothing estimated posterior<jupyter_code>mtype = []
ib.mask = ib.generate_mask(h=0.1)

for ii in range(2): #S-XOR and spiral
    mtype.append([])
    
    for jj in range(1,4):
        if jj == 3:
            mtype_i = np.column_stack([ib.human[ii][:,3], ib.human[ii][:,5], ib.human[ii][:,0]]) # human estimates
        else:
            mtype_i = ib.estpst_sample[ii][jj] # ML estimates        
        
        mtype_i = ib.pointwise_gridAverage(mtype_i).to_numpy()        
        xy, original, down, alls = ib.smooth_gaussian_distance(mtype_i, step=0.01, method=None, sigma=1, k=10)
        mtype[ii].append(1-alls)<jupyter_output><jupyter_text>Generate compiled plot and output images<jupyter_code>angle_step = 12
new_dtype = ['Original'] + dtype
fdtype = ['spiral'] #dtype defined for filename

palette = sns.color_palette('bright', len(ib.mtype))
palette = sns.color_palette(['c','k','r'], as_cmap=True)

col = 5 #CHANGED
row = 2

step = 0.2
r = 4
x_range = np.arange(-r,r,step)

for sw, lb in enumerate(fdtype):
    # for deg in np.arange(0, 180+angle_step, angle_step):
    for deg in [84]:

        line_plot = []
        line_post = []

        fig, axs = plt.subplots(row,col,figsize=(5*(col+2),6*row))
        gs = axs[0, 3].get_gridspec() #CHANGED HERE

        # remove the underlying axes
        for ax in axs[:, -1]:
            ax.remove()

        for ax in axs[:, -2]:
            ax.remove()

        for i in range(2):    
            line_plot.append([])
            line_post.append([])

            for j in range(3):
                # if i == 0: #exclude SVM
                #     continue

                line_plot[i].append([])
                line_post[i].append(ib.select_linear_region(mtype[i][j], degree=deg, step=0.001))
                
                lp, li = line_post[i][j] #line posterior, line index
                lp = 1 - lp #reversing class
                x = ib.mask[li][:,0]
                y = ib.mask[li][:,1]
                dist = np.sqrt(x**2 + y**2)

                dist[y < 0] *= -1 # negative radial distance wrt y-coordinate

                for rad in x_range:
                    line_plot[i][j].append(np.array(lp[(dist >= rad) * (dist < rad+step)]).mean())

        for m in range(2):
            for i in range(3):
                # if i == 0: #exclude SVM
                #     continue
                
                if m == 0:
                    im = axs[m,i].scatter(ib.mask[:,0], ib.mask[:,1], c=mtype[sw][i], cmap=cmap, vmin=0, vmax=1) #switch here
                    # fig.colorbar(im, ax=axs[m,i])
                    
                else:
                    ibXY = ib.mask[line_post[m-1][i][1]]
                    im = axs[m,i].scatter(ibXY[:,0], ibXY[:,1], c=line_post[sw][i][0], cmap=cmap, vmin=0, vmax=1)

                cbar = fig.colorbar(im, ax=axs[m,i])
                # cbar.ax.set_yticklabels(cbar.ax.get_yticklabels(), fontsize=20)
                for t in cbar.ax.get_yticklabels():
                    t.set_fontsize(20)

                # figure styling
                if m == 0:
                    axs[m,i].set_title(label[i], fontsize=fsize)

                # if i == 0:
                #     axs[m,i].set_ylabel(new_dtype[m], fontsize=fsize)

                axs[m,i].set_xlim(-3,3)
                axs[m,i].set_ylim(-3,3)
                # axs[m,i].set_xticklabels(fontsize=18)
                # axs[m,i].set_yticklabels(fontsize=18)
                axs[m,i].tick_params(axis='both', labelsize=20, length=10)
                # axs[m,i].tick_params(axis='y', labelsize=20)
                
                
        axbig = fig.add_subplot(gs[:, -2:])

        tempdf = pd.DataFrame(np.array(line_plot[sw]).T, columns=label) #switch here
        tempdf.index = x_range
        for i, lab in enumerate(label):
            sns.lineplot(data=tempdf[[lab]], ax=axbig, palette=[palette[i]]) #palette[[0,2,3][i]]

        axbig.legend(loc=0, fontsize=25)
        axbig.set_title(['Mean Linear Posterior at 82 degrees', 'S-XOR'][sw], fontsize=fsize)
        axbig.set_ylim([-0.1,1.1])
        axbig.set_xlim(-3,3)
        axbig.tick_params(axis='both', labelsize=20, length=10)

        if SAVEFIG:  
            plt.savefig(f'figs/[20220203_generate_figure_for_kdg_paper]_human_experiment_spiral.pdf', bbox_inches='tight')

        plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13940336490880814, 'fraction_numerical': 0.02714548282199915, 'mean_word_length': 2.8279220779220777, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 38, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '224004', 'license': 'MIT', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2528, 'n_words': 497, 'path': 'experiments/20220203_generate_figure_for_kdg_paper.ipynb', 'repo_name': 'jshin13/Inductive-bias-experiment', 'size': '7073'}