query_dict = {'expansions__vectors__rep': 0, 
              'expansions__k':3,
              'labelled':'amazon_grouped-tagged',
            'expansions__use_similarity': 0, 
            'expansions__neighbour_strategy':'linear',
            'expansions__vectors__dimensionality': 100, 
            'document_features_ev': 'AN+NN', 
            'document_features_tr': 'J+N+AN+NN', 
            'expansions__allow_overlap': False,
            'expansions__entries_of': None,
            'expansions__vectors__algorithm': 'glove',
            'expansions__vectors__composer__in': ['Left'],
            'expansions__vectors__unlabelled': 'wiki',
              'expansions__vectors__unlabelled_percentage':100,
            'expansions__decode_handler': 'SignifiedOnlyFeatureHandler',
            'expansions__noise': 0}
ids = Experiment.objects.filter(**query_dict).order_by('expansions__vectors__unlabelled_percentage',
                                                           'expansions__vectors__composer').values_list('id', flat=True)

ids

get_ci(ids[0])[:-1]

results = Results.objects.get(id=ids[0], classifier='MultinomialNB')
pred = results.predictions
gold = results.gold

from sklearn.metrics import classification_report, confusion_matrix
from sklearn.utils.multiclass import unique_labels
print(classification_report(gold, pred))

sns.set_style('white')
plot_confusion_matrix(gold, pred)

"""
Explanation: Is there a difference in the precision-recall values of different models?
End of explanation
"""


path = '../FeatureExtractionToolkit/word2vec_vectors/composed/AN_NN_word2vec-wiki_100percent-rep0_Add.events.filtered.strings.kmeans2000'
df = pd.read_hdf(path, key='clusters')

counts = df.clusters.value_counts()
g = sns.distplot(counts.values, kde_kws={'cut':True})
g.set(xlim=(0, None))
plt.title('Distribution of cluster sizes, k=500');

counts.describe()

"""
Explanation: Overall, precision and recall are balanced and roughly equal. Better models are better in both P and R.
What's in each cluster when using VQ?
End of explanation
"""


df[df.clusters==counts.argmin()].head(20)

df[df.clusters == 5]
# cluster 5 (negative sentiment), 2 (royalty), 8 (cheap, expencive) are very sensible
# cluster 3 ('arm'), 1 ('product'), 15 (hot), 16 (playing) are dominated by a single word (may contain multiple senses, e.g. hot water, hot waiter)
# cluster 6 (grand slam, grand prix, door slam) dominated by a few words and a polysemous word bridging senses
# cluster 10- film characters + misc
# 11 - sentiment, mix of positive and negative
# 13- named entities
# 14- arche, tower, veranda + related words + other senses (arch enemy)

from collections import Counter
Counter([str(x).split('_')[0] for x in df[df.clusters == 5].index]).most_common(10)

"""
Explanation: Find the smallest cluster and print phrases it contains
End of explanation
"""


from discoutils.thesaurus_loader import Vectors as vv
# not quite the same vectors (15% vs 100%), but that's all I've got on this machine
v = vv.from_tsv('../FeatureExtractionToolkit/word2vec_vectors/composed/AN_NN_word2vec-wiki_15percent-rep0_Add.events.filtered.strings')

v.init_sims(n_neighbors=30)

v.get_nearest_neighbours('bad/J')[:5]

"""
Explanation: Large clusters form (this one is 934 unigrams and NP) that share a word (e.g. bad occurs in 726 of those), and even though they are not pure (e.g. good is also in that cluster 84 times) the vast majority of "bad" stuff ends up in cluster 5, which starts to correspond to negative sentiment. This can be something the classifier picks up on.
For comparison, nearest neighbours using a KDTree
End of explanation
"""


cluster_num = df.ix['good/J_guy/N'][0]
print(cluster_num)
df[df.clusters == cluster_num]

Counter([str(x).split('_')[0] for x in df[df.clusters == cluster_num].index]).most_common(10)

cluster_num = df.ix['good/J_movie/N'][0]
print(cluster_num)
df[df.clusters == cluster_num]

Counter([str(x).split('_')[1] for x in df[df.clusters == cluster_num].index]).most_common(10)

df[df.clusters == counts.argmax()] # these appear to be names, they are 99% unigrams

"""
Explanation: Let's find if there is a positive sentiment cluster
End of explanation
"""


path = '../FeatureExtractionToolkit/socher_vectors/composed/AN_NN_turian_Socher.events.filtered.strings.kmeans2000'
ddf = pd.read_hdf(path, key='clusters')

cluster_num = ddf.ix['bad/J_guy/N'][0]
print(cluster_num)
ddf[ddf.clusters == cluster_num]

Counter([str(x).split('_')[1] for x in ddf[ddf.clusters == cluster_num].index]).most_common(10)

"""
Explanation: Does the same hold for Turian vectors?
End of explanation
"""


gaps = []
for r in Results.objects.filter(classifier=CLASSIFIER):
    gap = r.accuracy_mean - r.macrof1_mean 
    if abs(gap) > 0.1:
        print(r.id.id)
    gaps.append(gap)
plt.hist(gaps);

"""
Explanation: Is it OK to use accuracy instead of Averaged F1 score?
End of explanation
"""


from discoutils.thesaurus_loader import Vectors
from discoutils.tokens import DocumentFeature
v = Vectors.from_tsv('../FeatureExtractionToolkit/word2vec_vectors/word2vec-wiki-15perc.unigr.strings.rep0')

from random import sample
sampled_words = sample(list(v.keys()), 5000)
v.init_sims(n_neighbors=100)

data = []
for w in sampled_words:
    doc_feat = DocumentFeature.from_string(w)
    if doc_feat.tokens[0].pos == 'N' and np.random.uniform() < 0.8:
        # too many nouns, ignore some of them
        continue
    neigh = v.get_nearest_neighbours(w)
    for rank, (n, sim) in enumerate(neigh):
        pospos = doc_feat.tokens[0].pos + DocumentFeature.from_string(n).tokens[0].pos
        data.append([''.join(pospos), sim, rank])

df = pd.DataFrame(data, columns='pospos sim rank'.split())
mask = df.pospos.str.len() == 2
df = df[mask]
df.pospos.value_counts(), df.shape

g = sns.FacetGrid(df, col='pospos', col_wrap=3);
g.map(plt.hist, 'sim');

g = sns.FacetGrid(df, col='pospos', col_wrap=3);
g.map(plt.hist, 'rank');

"""
Explanation: Are neighbours of words other words, and is there grouping by PoS tag?
End of explanation
"""


