<jupyter_start><jupyter_text>paths<jupyter_code>SOURCE_DATA_ROOT = pyprojroot.here('results/searchstims/source_data/')

FIGURES_ROOT = pyprojroot.here('docs/paper/figures/experiment-1/searchstims-10stims-multiple-source-datasets')<jupyter_output><empty_output><jupyter_text>constants<jupyter_code>LEARNING_RATE = 1e-3

NET_NAMES = [
    'alexnet',
    'VGG16',
]

METHODS = [
    'initialize',
    'transfer'
]

MODES = [
    'classify',
]
sns.set_palette("Set2")<jupyter_output><empty_output><jupyter_text>load / tidy source data<jupyter_code>df_all = []

for source_data_dir, pretrained in zip(
    ['10stims', '10stims_random_weights', '10stims_SIN', '10stims_Clipart', '10stims_searchstims_classify'],
    ['ImageNet', 'random weights', 'Stylized ImageNet', 'DomainNet, Clipart domain', 'Searchstims (classify)']
):

    df = pd.read_csv(SOURCE_DATA_ROOT / source_data_dir / 'all.csv')

    df = df[df.net_name.isin(NET_NAMES)]

    if source_data_dir == '10stims':
        df['dataset'] = np.nan
        df.loc[df['method'] == 'transfer', 'dataset'] = pretrained
        df.loc[df['method'] == 'initialize', 'dataset'] = 'Searchstims'
    else:
        df['dataset'] = pretrained

    df['method, dataset'] = df['method'] + ', ' + df['dataset']

    df_all.append(df)

df_all = pd.concat(df_all)

df_all = df_all[df_all.target_condition == 'both']

# map set sizes to consecutive integers
set_size_uniq = df_all.set_size.unique()
set_size_map = dict(zip(set_size_uniq, range(len(set_size_uniq))))
df_all['set_size_int'] = df_all.set_size.map(set_size_map)<jupyter_output><empty_output><jupyter_text>drop rows from ('initialize, Searchstims') training replicates that failed to convergeto avoid cluttering figure with weird looking mean<jupyter_code>print(f'`df_all` length before dropping rows: {len(df_all)}')

for net_name in df_all.net_name.unique():
    for net_number in range(1, 9):
        this_df = df_all[
            (df_all.method == 'initialize') & 
            (df_all.dataset == 'Searchstims') & 
            (df_all.net_name == net_name) &
            (df_all.net_number == net_number)
        ]
        if (this_df.accuracy < 0.6).any():
            print(f'dropping {net_name}, replicate {net_number}, {len(this_df)} rows')
            df_all = df_all.drop(index=this_df.index)

print(f'`df_all` length after dropping rows: {len(df_all)}')
if '3stims' in str(FIGURES_ROOT):
    FIGSIZE = tuple(cm_to_inches(size) for size in (7, 5))
elif '10stims' in str(FIGURES_ROOT):
    FIGSIZE = tuple(cm_to_inches(size) for size in (20, 5))

DPI = 300

FIG_ROWS = df_all.net_name.unique()
FIG_COLUMNS = df_all.stimulus.unique()

n_rows = len(FIG_ROWS)
n_cols = len(FIG_COLUMNS)
fig, ax_arr = plt.subplots(n_rows, n_cols, sharey=True, figsize=FIGSIZE, dpi=DPI)
# fig.subplots_adjust(hspace=0.5)

LABELSIZE = 6
XTICKPAD = 2
YTICKPAD = 1

for ax_ in ax_arr.ravel():
    ax_.xaxis.set_tick_params(pad=XTICKPAD, labelsize=LABELSIZE)
    ax_.yaxis.set_tick_params(pad=YTICKPAD, labelsize=LABELSIZE)
    

STIM_FONTSIZE = 4

add_legend = False

for row, net_name in enumerate(FIG_ROWS):
    df_this_net = df_all[df_all['net_name'] == net_name]

    for col, stim_name in enumerate(FIG_COLUMNS):
        df_this_net_this_stim = df_this_net[df_this_net.stimulus == stim_name]

        ax = ax_arr[row, col]
        ax.set_axisbelow(True)  # so grid is behind

        if row == 0 and col == 0:
            legend = True
        else:
            legend = False

        sns.lineplot(
            x='set_size_int',
            y='accuracy',
            data=df_this_net_this_stim, 
            hue='dataset', 
            style='method',
            ax=ax,
            legend=legend
        )
        ax.set_ylabel('')
        ax.set_xlabel('')
        ax.set_xticks(range(len(set_size_uniq)))
        ax.set_xlim([-0.25, len(set_size_uniq) - 0.75])
        ax.set_xticklabels(set_size_uniq)

        if row == (n_rows - 1):
            ax.set_xticklabels(set_size_uniq)
        else:
            ax.set_xticklabels('')

        if row == 0:
            title = stim_name.replace('_',' ')
            ax.set_title(title, 
                         fontsize=STIM_FONTSIZE,
                         pad=5)  # pad so we can put image over title without it showing
        if col == 0:
            net_name_for_fig = net_name.replace('_', ' ')
            if net_name_for_fig == 'alexnet':
                net_name_for_fig = 'AlexNet'
            ax.text(0.075, 0.4, net_name_for_fig, fontweight='bold', fontsize=8)

# add a big axis, hide frame
big_ax = fig.add_subplot(111, frameon=False)
# hide tick and tick label of the big axis
big_ax.set_xticks([])
big_ax.set_yticks([])
big_ax.tick_params(labelcolor='none', top=False, bottom=False, left=False, right=False)
big_ax.grid(False)

handles, labels = ax_arr[0, 0].get_legend_handles_labels()
ax_arr[0, 0].get_legend().remove()

LEGEND_FONTSIZE = 5

if '3stims' in str(FIGURES_ROOT):
    BBOX_TO_ANCHOR = (1.525, 0.3)
elif '10stims' in str(FIGURES_ROOT):
    BBOX_TO_ANCHOR = (1.225, 0.75)

# big_ax.legend(handles,
#               labels, 
#               bbox_to_anchor=BBOX_TO_ANCHOR,
#               frameon=True,
#               borderaxespad=0., 
#               fontsize=LEGEND_FONTSIZE
#              );

big_ax.set_xlabel("Set size", labelpad=10);
big_ax.set_ylabel('Accuracy', labelpad=10);

for ext in ('svg', 'png'):
    fig_path = FIGURES_ROOT.joinpath(
        f'acc-v-set-size/acc-v-set-size.{ext}'
    )
    plt.savefig(fig_path,
                bbox_inches="tight")
for h, l in zip(handles, labels):
    print(l, h.get_color())<jupyter_output>dataset w
Searchstims (0.4, 0.7607843137254902, 0.6470588235294118)
ImageNet (0.9882352941176471, 0.5529411764705883, 0.3843137254901961)
random weights (0.5529411764705883, 0.6274509803921569, 0.796078431372549)
Stylized ImageNet (0.9058823529411765, 0.5411764705882353, 0.7647058823529411)
DomainNet, Clipart domain (0.6509803921568628, 0.8470588235294118, 0.32941176470588235)
Searchstims (classify) (1.0, 0.8509803921568627, 0.1843137254901961)
method w
initialize .2
transfer .2
{'fraction_non_alphanumeric': 0.1310129699556723, 'fraction_numerical': 0.06025283204728288, 'mean_word_length': 3.332859174964438, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '545725', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2569, 'n_tokens_neox': 2294, 'n_words': 402, 'path': 'src/scripts/experiment-1-searchstims/10stims-acc-v-set-size-multiple-source-datasets.ipynb', 'repo_name': 'NickleDave/Nicholson-Prinz-2020', 'size': '6091'}