# ---
# 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
# ---

# +
# %load_ext autoreload
# %autoreload 2

import os
import pandas as pd
import qgrid
import gensim
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import normalize
import nltk
os.chdir(os.getcwd() + '/..')
from data_io import *
from speech import *
import time
import matplotlib.pyplot as plt


# -

# Read data.
labeled = load_data()
unlabeled = load_unlabeled_data()
all_data = labeled.data + unlabeled.data

# # Example

# Compute embeddings of labeled data to use as seed for means
embedding_vectorizer = lambda : W2VAggVectorizerAvgTfidf(tfidf=TfidfVectorizer(tokenizer=nltk.word_tokenize, max_df=0.8), window=20, dim=200)
labeled_embeddings = embedding_vectorizer().fit_transform(np.array(labeled.data))

df = pd.DataFrame(labeled_embeddings)
df['y'] = labeled.labels
labeled_centroids = df.groupby('y').mean()
labeled_centroids

cosine_similarities = pd.DataFrame(cosine_similarity(labeled_centroids.values)).set_index(labeled_centroids.index)
cosine_similarities

plt.imshow(cosine_similarities)
plt.title('Cosine similarities')
plt.colorbar()

dotproduct_similarities = labeled_centroids.dot(labeled_centroids.T)
dotproduct_similarities

plt.imshow(dotproduct_similarities)
plt.title('Dot product similarities')
plt.colorbar()

# +
fig, axs = plt.subplots(2, 1, figsize=(5,10))

im = axs[0].imshow(cosine_similarities)
fig.colorbar(im, ax=axs[0])
axs[0].set_xticks([])
axs[0].set_yticks([])
axs[0].set_xlabel('candidates')
axs[0].set_ylabel('candidates')
axs[0].set_title('Pairwise cosine similatiries')

im = axs[1].imshow(dotproduct_similarities)
fig.colorbar(im, ax=axs[1])
axs[1].set_xticks([])
axs[1].set_yticks([])
axs[1].set_xlabel('candidates')
axs[1].set_ylabel('candidates')
axs[1].set_title('Pairwise dotproduct similatiries')

# plt.imshow(cosine_similarities)
# plt.title('Cosine similarities')
# plt.colorbar()
# -

# # AvgW2V_tfidf

labeled_embeddings = {}
for i, window in enumerate([5, 10, 15]):
    for j, dim in enumerate([100, 150, 200]):
        # Compute embeddings of labeled data to use as seed for means
        embedding_vectorizer = lambda : TfidfEmbeddingVectorizer(tokenizer=nltk.word_tokenize, window=window, dim=dim)
        labeled_embeddings[f'w{window}_d{dim}'] = embedding_vectorizer().fit_transform(np.array(labeled.data))

fig, axs = plt.subplots(3, 3, figsize=(20,20))
for i, window in enumerate([5, 10, 15]):
    for j, dim in enumerate([100, 150, 200]):
        # Get centroids for each candidate
        df = pd.DataFrame(labeled_embeddings[f'w{window}_d{dim}'])
        df['y'] = labeled.labels
        labeled_centroids = df.groupby('y').mean()
        
        # Get similarities
        cosine_similarities = pd.DataFrame(cosine_similarity(labeled_centroids.values)).set_index(labeled_centroids.index)
        
        # Plot
        im = axs[i, j].imshow(cosine_similarities)
        fig.colorbar(im, ax=axs[i, j])

fig, axs = plt.subplots(3, 3, figsize=(20,20))
for i, window in enumerate([5, 10, 15]):
    for j, dim in enumerate([100, 150, 200]):
        # Get centroids for each candidate
        df = pd.DataFrame(labeled_embeddings[f'w{window}_d{dim}'])
        df['y'] = labeled.labels
        labeled_centroids = df.groupby('y').mean()
        
        # Get similarities
        dotproduct_similarities = labeled_centroids.dot(labeled_centroids.T)
        
        # Plot
        im = axs[i, j].imshow(dotproduct_similarities)
        fig.colorbar(im, ax=axs[i, j])

# # AvgW2V

avgw2v = {}
for i, window in enumerate([5, 10, 15]):
    for j, dim in enumerate([100, 150, 200]):
        # Compute embeddings of labeled data to use as seed for means
        avgw2v_vectorizer = lambda : AvgW2VVectorizer(tokenizer=nltk.word_tokenize, window=window, dim=dim)
        avgw2v[f'w{window}_d{dim}'] = avgw2v_vectorizer().fit_transform(np.array(labeled.data))

fig, axs = plt.subplots(3, 3, figsize=(20,20))
for i, window in enumerate([5, 10, 15]):
    for j, dim in enumerate([100, 150, 200]):
        # Get centroids for each candidate
        df = pd.DataFrame(avgw2v[f'w{window}_d{dim}'])
        df['y'] = labeled.labels
        labeled_centroids = df.groupby('y').mean()
        
        # Get similarities
        dotproduct_similarities = labeled_centroids.dot(labeled_centroids.T)
        
        # Plot
        im = axs[i, j].imshow(dotproduct_similarities)
        fig.colorbar(im, ax=axs[i, j])

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, axs = plt.subplots(2, 2, figsize=(20,20))
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Axis [0, 0]')
axs[0, 1].plot(x, y, 'tab:orange')
axs[0, 1].set_title('Axis [0, 1]')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 0].set_title('Axis [1, 0]')
axs[1, 1].plot(x, -y, 'tab:red')
axs[1, 1].set_title('Axis [1, 1]')


# Determine weights proportional to affiliation from labels
def weights_from_labeled():
    df = pd.DataFrame(labeled.labels)
    df['a'] = 1
    return df.groupby(0).sum() / len(labeled.labels)


# +
pipe_19_glove_labelinit = Pipeline([
  (f'avgw2v_tfidf', embedding_vectorizer_glove()),
  ('gmm', GaussianMixture(n_components=19,
                    init_params='random',
                    weights_init=weights_from_labeled().values.ravel(),
                    means_init=labeled_centroids.values,
                    verbose=10))
])

print("Computing clusters...", end='')
pipe_19_glove_labelinit.fit(np.array(all_data))
print("done")
print("%0.2fs" % (time.time() - t0))
# -

gmm_19_glove_labelinit = pipe_19_glove_labelinit['gmm']
X_labeled_19_glove_labelinit = pipe_19_glove_labelinit['avgw2v_tfidf'].transform(np.array(labeled.data))
probs_19_glove_labelinit = gmm_19_glove_labelinit.predict_proba(X_labeled_19_glove_labelinit)

# # Stats GMM random seed

df = pd.DataFrame(probs_19_glove_rnd)
df['y'] = labeled.labels
candidates_probs_rnd = df.groupby('y').mean()
candidates_probs_rnd

plt.imshow(candidates_probs_rnd)
plt.colorbar()

candidates_probs_rnd.std(axis=1)

print(candidates_probs_rnd.std(axis=1).values.mean())
plt.imshow(np.expand_dims(candidates_probs_rnd.std(axis=1).values, axis=0))
plt.colorbar()

df = pd.DataFrame(pipe_19_glove_rnd.predict(np.array(all_data)))
df[1] = 1
df.groupby(0).sum().plot.bar()

# # Stats GMM labeled centroids seed

df = pd.DataFrame(probs_19_glove_labelinit)
df['y'] = labeled.labels
candidates_probs_labelinit = df.groupby('y').mean()
candidates_probs_labelinit

plt.imshow(candidates_probs_labelinit)
plt.colorbar()

candidates_probs_labelinit.std(axis=1)

print(candidates_probs_rnd.std(axis=1).values.mean())
plt.imshow(np.expand_dims(candidates_probs_labelinit.std(axis=1).values, axis=0))
plt.colorbar()

df = pd.DataFrame(pipe_19_glove_rnd.predict(np.array(all_data)))
df[1] = 1
df.groupby(0).sum().plot.bar()


class A:
    def __init__(self):
        setattr(self, 'a', 1)
A().a

StarcoderdataJupyterScriptsDedupFiltered