from cStringIO import StringIO
from nose.tools import raises
from microscopes.lda import utils


def test_docs_from_document_term_matrix():
    dtm = [[2, 1], [3, 2]]
    docs = [[0, 0, 1], [0, 0, 0, 1, 1]]
    assert utils.docs_from_document_term_matrix(dtm) == docs


def test_docs_from_ldac_simple():
    stream = StringIO()
    stream.write("2 0:2 1:1\n2 0:3 1:2")
    stream.seek(0) # rewind stream
    docs = [[0, 0, 1], [0, 0, 0, 1, 1]]
    assert utils.docs_from_ldac(stream) == docs

    stream = StringIO()
    stream.write("2 1:1 0:2\n3 2:1 0:3 1:1")
    stream.seek(0) # rewind stream
    docs = [[1, 0, 0], [2, 0, 0, 0, 1]]
    assert utils.docs_from_ldac(stream) == docs


@raises(AssertionError)
def test_bad_ldac_data():
    stream = StringIO()
    stream.write("2 0:1")
    stream.seek(0) # rewind stream
    utils.docs_from_ldac(stream)

import numpy as np

from cStringIO import StringIO
from nose.tools import raises
from microscopes.lda import utils


def test_docs_from_document_term_matrix():
    dtm = [[2, 1], [3, 2]]
    docs = [[0, 0, 1], [0, 0, 0, 1, 1]]
    assert utils.docs_from_document_term_matrix(dtm) == docs


def test_docs_from_numpy_dtp():
    dtm = np.array([[2, 1], [3, 2]])
    docs = [[0, 0, 1], [0, 0, 0, 1, 1]]
    assert utils.docs_from_document_term_matrix(dtm) == docs


def test_docs_from_ldac_simple():
    stream = StringIO()
    stream.write("2 0:2 1:1\n2 0:3 1:2")
    stream.seek(0) # rewind stream
    docs = [[0, 0, 1], [0, 0, 0, 1, 1]]
    assert utils.docs_from_ldac(stream) == docs

    stream = StringIO()
    stream.write("2 1:1 0:2\n3 2:1 0:3 1:1")
    stream.seek(0) # rewind stream
    docs = [[1, 0, 0], [2, 0, 0, 0, 1]]
    assert utils.docs_from_ldac(stream) == docs


@raises(AssertionError)
def test_bad_ldac_data():
    stream = StringIO()
    stream.write("2 0:1")
    stream.seek(0) # rewind stream
    utils.docs_from_ldac(stream)

Create docs from numpy