from __future__ import division

import sys
import time
import logging
import StringIO
import numpy as np
from numpy import array, zeros, allclose
import tensorflow as tf

logger = logging.getLogger("hw3")
logger.setLevel(logging.DEBUG)


# logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)


def variable_summaries(var, name_scope, matrix=True):
    """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""

    with tf.name_scope(name_scope):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)
        if matrix:
            norm = tf.sqrt(tf.reduce_sum(var * var))
            tf.summary.scalar('norm', norm)


def load_preprocess_data(data_dir, mode_story=None, size=None):
    # read and assemble training data
    train_context = read_data(data_dir + '/train.context', mode_story, size)
    train_question = read_data(data_dir + '/train.question', mode_story, size)
    train_answer = read_data_answer(data_dir + '/train.answer', size)
    train_data = vectorize(train_context, train_question, train_answer)
    print "Finished reading %d training data" % len(train_data)

    # read and assemble val data
    val_context = read_data(data_dir + '/val.context', mode_story, size)
    val_question = read_data(data_dir + '/val.question', mode_story, size)
    val_answer = read_data_answer(data_dir + '/val.answer', size)