#!/usr/bin/env python

import smurff
import matrix_io as mio
import pandas as pd
import unittest
import os
import sys
import tempfile
from subprocess import call
from time import time

global_verbose = False

class TestExCAPE_py(unittest.TestCase):
    def get_default_opts(self):
        return {
                "Ytest"           : TestExCAPE_py.data["test.sdm"],
                "priors"          : [ "normal", "normal" ],
                "num_latent"      : 16,
                "burnin"          : 400,
                "nsamples"        : 200,
                "Ynoise"          : ("fixed", 1.0, 0.0, 0.0, 0.0),
                "verbose"         : global_verbose, 
                "aux_data"        : [ [], [] ],
                "side_info"       : [ None, None ],
                "side_info_noises": [ ("fixed", 10.0, 0.0, 0.0, 0.0), ("fixed", 10.0, 0.0, 0.0, 0.0) ],
                }

    @classmethod
    def setUpClass(cls):
        files = [
                "train.sdm",
                "test.sdm",
                "side_c2v.ddm",
                "side_ecfp6_counts_var005.sdm",
                "side_ecfp6_folded_dense.ddm"
                ]

        cls.data = { f : mio.read_matrix(f) for f in files }

    def macau(self, side_info, direct, expected):
        args = self.get_default_opts()
        args["direct"] = direct

        for d in range(2):
            if side_info[d] != None:
                args["side_info"][d] = TestExCAPE_py.data[side_info[d]]
                args["priors"][d] = 'macau'
            else:
                args["side_info"][d] = None


        start = time()
        result = smurff.smurff(TestExCAPE_py.data["train.sdm"], **args)
        stop = time()
        elapsed = stop - start

        self.assertLess(result.rmse, expected[0])
        self.assertGreater(result.rmse, expected[1])
        self.assertLess(elapsed, expected[2])

    def test_bpmf(self):
        side_info = [ None, None ]
        self.macau(side_info, True, [ 1.22, 1.10, 120. ])

    def test_macau_c2v(self):
        side_info = [ "side_c2v.ddm", None ]
        self.macau(side_info, True, [1.1, 1.0, 240. ])

    def test_macau_ecfp_sparse_direct(self):
        side_info = [ "side_ecfp6_counts_var005.sdm", None ]
        self.macau(side_info, True, [1.19, 1.0, 1500. ])

    def test_macau_ecfp_sparse_cg(self):
        side_info = [ "side_ecfp6_counts_var005.sdm", None ]
        self.macau(side_info, False, [1.19, 1.0, 480. ])

    def test_macau_ecfp_dense(self):
        side_info = [ "side_ecfp6_folded_dense.ddm", None ]
        self.macau(side_info, True, [ 1.08, 1.0, 240. ])

if __name__ == "__main__":
    for arg in sys.argv:
        if (arg == "-v" or arg == "--verbose"):
            global_verbose = True
    unittest.main()

@@ -195,21 +195,17 @@
 rbose = 
-False
+1
 %0A%0Aclass 
@@ -291,76 +291,8 @@
 n %7B%0A
-                %22Ytest%22           : TestExCAPE_py.data%5B%22test.sdm%22%5D,%0A
     
@@ -490,332 +490,200 @@
    %22
-Ynoise%22          : (%22fixed%22, 1.0, 0.0, 0.0, 0.0),%0A                %22verbose%22         : global_verbose, %0A                %22aux_data%22        : %5B %5B%5D, %5B%5D %5D,%0A                %22side_info%22       : %5B None, None %5D,%0A                %22side_info_noises%22: %5B (%22fixed%22, 10.0, 0.0, 0.0, 0.0), (%22fixed%22, 10.0, 0.0, 0.0, 0.0) %5D,%0A                %7D
+verbose%22         : global_verbose, %0A                %7D%0A%0A    def get_train_noise(self):%0A        return smurff.FixedNoise(1.0)%0A%0A    def get_side_noise(self):%0A        return smurff.FixedNoise(10.)
 %0A%0A  
@@ -1096,40 +1096,8 @@
 ts()
-%0A        args%5B%22direct%22%5D = direct
 %0A%0A  
@@ -1184,142 +1184,364 @@
 gs%5B%22
-side_info%22%5D%5Bd%5D = TestExCAPE_py.data%5Bside_info%5Bd%5D%5D%0A                args%5B%22priors%22%5D%5Bd%5D = 'macau'%0A            else:%0A                args%5B%22
+priors%22%5D%5Bd%5D = 'macau'%0A%0A        session = smurff.PySession(**args)%0A        Ytrain = TestExCAPE_py.data%5B%22train.sdm%22%5D%0A        Ytest = TestExCAPE_py.data%5B%22test.sdm%22%5D%0A        session.addTrainAndTest(Ytrain, Ytest, self.get_train_noise())%0A%0A        for d in range(2):%0A            if side_info%5Bd%5D != None:%0A                session.addSideInfo(d, TestExCAPE_py.data%5B
 side
@@ -1549,116 +1549,172 @@
 info
-%22%5D
 %5Bd%5D
- = None%0A%0A%0A        start = time()%0A        result = smurff.smurff(TestExCAPE_py.data%5B%22train.sdm%22%5D, **args
+%5D, self.get_side_noise(), direct = direct)%0A%0A        session.init()%0A%0A        start = time()%0A        while session.step(): pass%0A        result = session.getResult(
 )%0A  

Fix excape python tests