# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd

import matplotlib.pyplot as plt
import seaborn
# %pylab inline

seaborn.set()

# +
import sys
sys.path.append("..")

import cfg
# -

NOISE_LEVELS = cfg.experiment.noise_levels; NOISE_LEVELS

# # IMDB

# ## CharCNN

results = pd.read_csv('../results/CharCNN_IMDB.csv', index_col=0)

results.sample(10)

# +
results = pd.read_csv('../results/CharCNN_IMDB.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('IMDB. Noised test set. CharCNN')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/FastText_IMDB.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('IMDB. Noised test set. FastText')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/YoonKim_IMDB.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('IMDB. Noised test set. CharCNN+RNN')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/AttentionedYoonKim_IMDB.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('IMDB. Noised test set. CharCNN+RNN+Attention')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()
# -

# # Mokoron

# +
results = pd.read_csv('../results/CharCNN_mokoron_backup.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    plot_data
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Mokoron. Noised test set. CharCNN')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/FastText_mokoron.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Mokoron. Noised test set. Fasttext')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/YoonKim_mokoron.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Mokoron. Noised test set. CharCNN+RNN')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/AttentionedYoonKim_mokoron.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Mokoron. Noised test set. CharCNN+RNN+Attention')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../../robust-w2v/results/rove_mokoron.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Mokoron. Noised test set. RoVe')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../../robust-w2v/results/rove_mokoron.csv', index_col=0)
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_orig[(results_orig.noise_level_train == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Mokoron. Noised test set. RoVe')
plt.xlabel('train noise level')
plt.ylabel('test f1')

plt.plot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()
# -

# ## Airline tweets

# +
results = pd.read_csv('../results/CharCNN_airline-tweets.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Airline tweets. Noised test set. CharCNN')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/FastText_airline-tweets.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Airline tweets. Noised test set. Fasttext')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../results/YoonKim_airline-tweets.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Airline tweets. Noised test set. CharCNN+RNN')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
results = pd.read_csv('../../robust-w2v/results/rove_airline_tweets.csv', index_col=0)
results_same = results[results['noise_level_test'] > 0]
results_orig = results[results['noise_level_test'] == -1]

plot_x = []
plot_y = []

j = 0
for i, noise_level in enumerate(NOISE_LEVELS):
    plot_data = results_same[(results_same.noise_level_train == noise_level) &\
                             (results_same.noise_level_test == noise_level)]
    if not plot_data.empty:
        plot_x.append(round(noise_level, 3))
        plot_y.append([])
        for _, res in plot_data.iterrows():
            plot_y[j].append(res['f1_test'])
        j += 1

plt.figure(figsize=(10, 8))
plt.title('Airline tweets. Noised test set. RoVe')
plt.xlabel('train noise level')
plt.ylabel('test f1')

seaborn.boxplot(plot_x, plot_y)
plt.savefig('IMDB.png')
plt.show()

# +
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.colors import colorConverter as cc
import numpy as np
 
def plot_mean_and_CI(mean, lb, ub, color_mean=None, color_shading=None):
    # plot the shaded range of the confidence intervals
    plt.fill_between(range(mean.shape[0]), ub, lb,
                     color=color_shading, alpha=.5)
    # plot the mean on top
    plt.plot(mean, color_mean)


class LegendObject(object):
    def __init__(self, facecolor='red', edgecolor='white', dashed=False):
        self.facecolor = facecolor
        self.edgecolor = edgecolor
        self.dashed = dashed
 
    def legend_artist(self, legend, orig_handle, fontsize, handlebox):
        x0, y0 = handlebox.xdescent, handlebox.ydescent
        width, height = handlebox.width, handlebox.height
        patch = mpatches.Rectangle(
            # create a rectangle that is filled with color
            [x0, y0], width, height, facecolor=self.facecolor,
            # and whose edges are the faded color
            edgecolor=self.edgecolor, lw=3)
        handlebox.add_artist(patch)
 
        # if we're creating the legend for a dashed line,
        # manually add the dash in to our rectangle
        if self.dashed:
            patch1 = mpatches.Rectangle(
                [x0 + 2*width/5, y0], width/5, height, facecolor=self.edgecolor,
                transform=handlebox.get_transform())
            handlebox.add_artist(patch1)
 
        return patch


# +
# generate 3 sets of random means and confidence intervals to plot
mean0 = np.random.random(50)
ub0 = mean0 + np.random.random(50) + .5
lb0 = mean0 - np.random.random(50) - .5

mean1 = np.random.random(50) + 2
ub1 = mean1 + np.random.random(50) + .5
lb1 = mean1 - np.random.random(50) - .5
 
mean2 = np.random.random(50) -1
ub2 = mean2 + np.random.random(50) + .5
lb2 = mean2 - np.random.random(50) - .5

# plot the data
fig = plt.figure(1, figsize=(7, 2.5))
plot_mean_and_CI(mean0, ub0, lb0, color_mean='k', color_shading='k')
plot_mean_and_CI(mean1, ub1, lb1, color_mean='b', color_shading='b')
plot_mean_and_CI(mean2, ub2, lb2, color_mean='g--', color_shading='g')

bg = np.array([1, 1, 1])  # background of the legend is white
colors = ['black', 'blue', 'green']
# with alpha = .5, the faded color is the average of the background and color
colors_faded = [(np.array(cc.to_rgb(color)) + bg) / 2.0 for color in colors]
 
plt.legend([0, 1, 2], ['Data 0', 'Data 1', 'Data 2'],
           handler_map={
               0: LegendObject(colors[0], colors_faded[0]),
               1: LegendObject(colors[1], colors_faded[1]),
               2: LegendObject(colors[2], colors_faded[2], dashed=True),
            })
 
plt.title('Example mean and confidence interval plot')
plt.tight_layout()
plt.grid()
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered