# Path: autoencoder/utils/io_utils.py
# def dump_json(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             json.dump(data, datafile)
#     except Exception as e:
#         raise e
# 
# Path: autoencoder/utils/op_utils.py
# def unitmatrix(matrix, norm='l2', axis=1):
#     if norm == 'l1':
#         maxtrixlen = np.sum(np.abs(matrix), axis=axis)
#     if norm == 'l2':
#         maxtrixlen = np.linalg.norm(matrix, axis=axis)
# 
#     if np.any(maxtrixlen <= 0):
#         return matrix
#     else:
#         maxtrixlen = maxtrixlen.reshape(1, len(maxtrixlen)) if axis == 0 else maxtrixlen.reshape(len(maxtrixlen), 1)
#         return matrix / maxtrixlen


import numpy as np
from gensim import corpora
from gensim.models.ldamodel import LdaModel
from ..utils.io_utils import dump_json
from ..utils.op_utils import unitmatrix

def show_topics(model, n_words_per_topic=10):
    n_topics = model.num_topics
    topics = [zip(*model.show_topic(i, n_words_per_topic))[0] for i in range(n_topics)]

    return topics

def show_topics_prob(model, n_words_per_topic=10):
    n_topics = model.num_topics
    topics = [model.show_topic(i, n_words_per_topic) for i in range(n_topics)]

    return topics

def calc_pairwise_cosine(model):
    n = model.num_topics
    weights = model.state.get_lambda()
    weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.
    weights = unitmatrix(weights) # normalize
    score = []
    for i in range(n):
        for j in range(i + 1, n):
            score.append(np.arccos(weights[i].dot(weights[j])))

    return np.mean(score), np.std(score)

def calc_pairwise_dev(model):
    # the average squared deviation from 0 (90 degree)
    n = model.num_topics
    weights = model.state.get_lambda()
    weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.

# Path: autoencoder/utils/io_utils.py
# def dump_json(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             json.dump(data, datafile)
#     except Exception as e:
#         raise e
# 
# Path: autoencoder/utils/op_utils.py
# def unitmatrix(matrix, norm='l2', axis=1):
#     if norm == 'l1':
#         maxtrixlen = np.sum(np.abs(matrix), axis=axis)
#     if norm == 'l2':
#         maxtrixlen = np.linalg.norm(matrix, axis=axis)
# 
#     if np.any(maxtrixlen <= 0):
#         return matrix
#     else:
#         maxtrixlen = maxtrixlen.reshape(1, len(maxtrixlen)) if axis == 0 else maxtrixlen.reshape(len(maxtrixlen), 1)
#         return matrix / maxtrixlen


# Path: autoencoder/baseline/lda.py
import numpy as np
from gensim import corpora
from gensim.models.ldamodel import LdaModel
from ..utils.io_utils import dump_json
from ..utils.op_utils import unitmatrix


def show_topics(model, n_words_per_topic=10):
    n_topics = model.num_topics
    topics = [zip(*model.show_topic(i, n_words_per_topic))[0] for i in range(n_topics)]

    return topics

def show_topics_prob(model, n_words_per_topic=10):
    n_topics = model.num_topics
    topics = [model.show_topic(i, n_words_per_topic) for i in range(n_topics)]

    return topics

def calc_pairwise_cosine(model):
    n = model.num_topics
    weights = model.state.get_lambda()
    weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.
    weights = unitmatrix(weights) # normalize
    score = []
    for i in range(n):
        for j in range(i + 1, n):
            score.append(np.arccos(weights[i].dot(weights[j])))

    return np.mean(score), np.std(score)

def calc_pairwise_dev(model):
    # the average squared deviation from 0 (90 degree)
    n = model.num_topics
    weights = model.state.get_lambda()
    weights = np.apply_along_axis(lambda x: x / x.sum(), 1, weights) # get dist.

    weights = unitmatrix(weights) # normalize