# Path: src/backend/math_utils.py
# def logistic_normal(mean, cov, n):
#     """
#     returns samples from a logistic normal distribution
#     @param mean: mean vector
#     @param cov: covariance matrix
#     @param n: number of samples
#     @return n samples from the logistic normal distribution
#     """
#     samples = np.random.multivariate_normal(mean,cov, n)
#     samples = np.exp(samples)
#     return samples/samples.sum(axis=1)[:, np.newaxis]


from itertools import combinations
from math import exp
from tmanalyzer import TMAnalyzer
from src.backend.math_utils import logistic_normal
from time import time
import os
import numpy as np
import pdb
        # doc-term (STD)
        wc_db = self.write_doc_term()

        # write the vocab to the database (STD)
        self.write_terms_table(wcs=wc_db)

        # write doc title to database (STD)
        self.write_docs_table()


        beta = np.loadtxt(os.path.join(self.params['outdir'],'final-log-beta.dat'))
        beta.shape = (self.params['ntopics'], len(beta)/self.params['ntopics'])

        # topic_terms
        self.write_topic_terms(beta)

        # topic_topic
        self.write_topic_topic(np.exp(beta))

        # term_term
        self.write_term_term(np.exp(beta))

        # doc_doc -- custom for CTM -- TODO perhaps port some of this code to helper methods/functions
        lam = np.loadtxt(os.path.join(self.params['outdir'],'final-lambda.dat'))
        lam.shape = (len(lam)/self.params['ntopics'], self.params['ntopics'])
        nu = np.loadtxt(os.path.join(self.params['outdir'],'final-nu.dat'))
        nu.shape = (len(nu)/self.params['ntopics'], self.params['ntopics'])
        sqrt_theta = []

        for i in xrange(len(lam)):

# Path: src/backend/math_utils.py
# def logistic_normal(mean, cov, n):
#     """
#     returns samples from a logistic normal distribution
#     @param mean: mean vector
#     @param cov: covariance matrix
#     @param n: number of samples
#     @return n samples from the logistic normal distribution
#     """
#     samples = np.random.multivariate_normal(mean,cov, n)
#     samples = np.exp(samples)
#     return samples/samples.sum(axis=1)[:, np.newaxis]


# Path: src/backend/ctmanalyzer.py
from itertools import combinations
from math import exp
from tmanalyzer import TMAnalyzer
from src.backend.math_utils import logistic_normal
from time import time
import os
import numpy as np
import pdb

        # doc-term (STD)
        wc_db = self.write_doc_term()

        # write the vocab to the database (STD)
        self.write_terms_table(wcs=wc_db)

        # write doc title to database (STD)
        self.write_docs_table()


        beta = np.loadtxt(os.path.join(self.params['outdir'],'final-log-beta.dat'))
        beta.shape = (self.params['ntopics'], len(beta)/self.params['ntopics'])

        # topic_terms
        self.write_topic_terms(beta)

        # topic_topic
        self.write_topic_topic(np.exp(beta))

        # term_term
        self.write_term_term(np.exp(beta))

        # doc_doc -- custom for CTM -- TODO perhaps port some of this code to helper methods/functions
        lam = np.loadtxt(os.path.join(self.params['outdir'],'final-lambda.dat'))
        lam.shape = (len(lam)/self.params['ntopics'], self.params['ntopics'])
        nu = np.loadtxt(os.path.join(self.params['outdir'],'final-nu.dat'))
        nu.shape = (len(nu)/self.params['ntopics'], self.params['ntopics'])
        sqrt_theta = []

        for i in xrange(len(lam)):

            samples = logistic_normal(lam[i,:], np.diag(nu[i,:]), n=100) # n=100 found adequate through aux experiments