<s> __author__ = "<STR_LIT>" <EOL> import os <EOL> import unittest <EOL> import pandas as pd <EOL> import numpy as np <EOL> from pkg_resources import resource_filename <EOL> import testfm <EOL> from testfm . models . graphchi_models import SVDpp <EOL> from testfm . models . tensorcofi import TensorCoFi , PyTensorCoFi <EOL> from testfm . models . baseline_model import IdModel , Item2Item , AverageModel <EOL> from testfm . models . ensemble_models import LogisticEnsemble <EOL> from testfm . models . content_based import TFIDFModel , LSIModel <EOL> from testfm . evaluation . evaluator import Evaluator <EOL> def which ( program ) : <EOL> """<STR_LIT>""" <EOL> def is_exe ( fpath ) : <EOL> if os . path . isfile ( fpath ) and os . access ( fpath , os . X_OK ) : <EOL> return True <EOL> fpath , fname = os . path . split ( program ) <EOL> if fpath : <EOL> if is_exe ( program ) : <EOL> return True <EOL> else : <EOL> for path in os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) : <EOL> path = path . strip ( "<STR_LIT>" ) <EOL> exe_file = os . path . join ( path , program ) <EOL> if is_exe ( exe_file ) : <EOL> return True <EOL> return False <EOL> class TestTensorCoFi ( unittest . TestCase ) : <EOL> def tearDown ( self ) : <EOL> import os <EOL> if os . path . exists ( "<STR_LIT>" ) : <EOL> os . remove ( "<STR_LIT>" ) <EOL> if os . path . exists ( "<STR_LIT>" ) : <EOL> os . remove ( "<STR_LIT>" ) <EOL> def setUp ( self ) : <EOL> self . df = pd . read_csv ( resource_filename ( testfm . __name__ , "<STR_LIT>" ) , sep = "<STR_LIT>" , header = None , <EOL> names = [ "<STR_LIT:user>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:date>" , "<STR_LIT:title>" ] ) <EOL> self . df = self . df . head ( n = <NUM_LIT:100> ) <EOL> def test_fit ( self ) : <EOL> tf = TensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> tf . fit ( self . df ) <EOL> self . assertEqual ( len ( self . df . user . unique ( ) ) , tf . factors [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( self . df . item . unique ( ) ) , tf . factors [ <NUM_LIT:1> ] . shape [ <NUM_LIT:0> ] ) <EOL> def test_refit ( self ) : <EOL> """<STR_LIT>""" <EOL> tf = TensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> tf . fit ( self . df ) <EOL> tf . fit ( self . df ) <EOL> self . assertEqual ( len ( self . df . user . unique ( ) ) , tf . factors [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( self . df . item . unique ( ) ) , tf . factors [ <NUM_LIT:1> ] . shape [ <NUM_LIT:0> ] ) <EOL> tf . fit ( self . df ) <EOL> self . assertEqual ( len ( self . df . user . unique ( ) ) , tf . factors [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( self . df . item . unique ( ) ) , tf . factors [ <NUM_LIT:1> ] . shape [ <NUM_LIT:0> ] ) <EOL> def test_ids_returns ( self ) : <EOL> tf = TensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> inp = [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> } ] <EOL> inp = pd . DataFrame ( inp ) <EOL> tf . fit ( inp ) <EOL> uid = tf . data_map [ tf . get_user_column ( ) ] [ <NUM_LIT:10> ] <EOL> iid = tf . data_map [ tf . get_item_column ( ) ] [ <NUM_LIT:100> ] <EOL> self . assertEquals ( uid , <NUM_LIT:0> ) <EOL> self . assertEquals ( iid , <NUM_LIT:0> ) <EOL> self . assertEquals ( len ( tf . factors [ <NUM_LIT:0> ] [ uid , : ] ) , tf . number_of_factors ) <EOL> self . assertEquals ( len ( tf . factors [ <NUM_LIT:1> ] [ iid , : ] ) , tf . number_of_factors ) <EOL> def test_score ( self ) : <EOL> tf = TensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> inp = [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> } ] <EOL> inp = pd . DataFrame ( inp ) <EOL> tf . fit ( inp ) <EOL> uid = tf . data_map [ tf . get_user_column ( ) ] [ <NUM_LIT:10> ] <EOL> iid = tf . data_map [ tf . get_item_column ( ) ] [ <NUM_LIT:100> ] <EOL> tf . factors [ <NUM_LIT:0> ] [ uid , <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> tf . factors [ <NUM_LIT:0> ] [ uid , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> tf . factors [ <NUM_LIT:1> ] [ iid , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> tf . factors [ <NUM_LIT:1> ] [ iid , <NUM_LIT:1> ] = <NUM_LIT:5> <EOL> self . assertEqual ( <NUM_LIT:0> * <NUM_LIT:1> + <NUM_LIT:1> * <NUM_LIT:5> , tf . get_score ( <NUM_LIT:10> , <NUM_LIT:100> ) ) <EOL> def test_fit_for_python_version ( self ) : <EOL> tf = PyTensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> tf . fit ( self . df ) <EOL> self . assertEqual ( len ( self . df . user . unique ( ) ) , tf . factors [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( self . df . item . unique ( ) ) , tf . factors [ <NUM_LIT:1> ] . shape [ <NUM_LIT:0> ] ) <EOL> def test_ids_returns_for_python_version ( self ) : <EOL> tf = PyTensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> inp = [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> } ] <EOL> inp = pd . DataFrame ( inp ) <EOL> tf . fit ( inp ) <EOL> uid = tf . data_map [ tf . get_user_column ( ) ] [ <NUM_LIT:10> ] <EOL> iid = tf . data_map [ tf . get_item_column ( ) ] [ <NUM_LIT:100> ] <EOL> self . assertEquals ( uid , <NUM_LIT:0> ) <EOL> self . assertEquals ( iid , <NUM_LIT:0> ) <EOL> self . assertEquals ( len ( tf . factors [ <NUM_LIT:0> ] [ uid , : ] ) , tf . number_of_factors ) <EOL> self . assertEquals ( len ( tf . factors [ <NUM_LIT:1> ] [ iid , : ] ) , tf . number_of_factors ) <EOL> def test_score_for_python_version ( self ) : <EOL> tf = PyTensorCoFi ( n_factors = <NUM_LIT:2> ) <EOL> inp = [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> } ] <EOL> inp = pd . DataFrame ( inp ) <EOL> tf . fit ( inp ) <EOL> uid = tf . data_map [ tf . get_user_column ( ) ] [ <NUM_LIT:10> ] <EOL> iid = tf . data_map [ tf . get_item_column ( ) ] [ <NUM_LIT:100> ] <EOL> tf . factors [ <NUM_LIT:0> ] [ uid , <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> tf . factors [ <NUM_LIT:0> ] [ uid , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> tf . factors [ <NUM_LIT:1> ] [ iid , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> tf . factors [ <NUM_LIT:1> ] [ iid , <NUM_LIT:1> ] = <NUM_LIT:5> <EOL> self . assertEqual ( <NUM_LIT:0> * <NUM_LIT:1> + <NUM_LIT:1> * <NUM_LIT:5> , tf . get_score ( <NUM_LIT:10> , <NUM_LIT:100> ) ) <EOL> class LogisticTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . le = LogisticEnsemble ( models = [ IdModel ( ) ] ) <EOL> inp = [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> } ] <EOL> self . df = pd . DataFrame ( inp ) <EOL> def test_construct_features ( self ) : <EOL> self . le . _prepare_feature_extraction ( self . df ) <EOL> self . assertEqual ( self . le . _user_count , { <NUM_LIT:10> : <NUM_LIT:2> , <NUM_LIT:12> : <NUM_LIT:1> } ) <EOL> def test_extract_features ( self ) : <EOL> self . le . _prepare_feature_extraction ( self . df ) <EOL> features = self . le . _extract_features ( <NUM_LIT:10> , <NUM_LIT:5> ) <EOL> self . assertEqual ( features , ( [ <NUM_LIT:2> , <NUM_LIT:5> ] , <NUM_LIT:1> ) ) <EOL> def test_prepare_data ( self ) : <EOL> X , Y = self . le . prepare_data ( self . df ) <EOL> self . assertEqual ( len ( X ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( len ( Y ) , <NUM_LIT:6> ) <EOL> def test_fit ( self ) : <EOL> self . le . fit ( self . df ) <EOL> self . assertIsNotNone ( self . le . model ) <EOL> def test_predict ( self ) : <EOL> self . le . fit ( self . df ) <EOL> self . assertIsInstance ( self . le . get_score ( <NUM_LIT:10> , <NUM_LIT> ) , float ) <EOL> class Item2ItemTest ( unittest . TestCase ) : <EOL> def test_fit ( self ) : <EOL> df = pd . DataFrame ( [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT:100> } ] ) <EOL> i2i = Item2Item ( ) <EOL> i2i . fit ( df ) <EOL> self . assertEqual ( i2i . _items [ <NUM_LIT:100> ] , set ( [ <NUM_LIT:10> , <NUM_LIT:12> ] ) ) <EOL> self . assertEqual ( i2i . _items [ <NUM_LIT> ] , set ( [ <NUM_LIT:10> ] ) ) <EOL> self . assertEqual ( i2i . _users [ <NUM_LIT:10> ] , set ( [ <NUM_LIT:100> , <NUM_LIT> ] ) ) <EOL> self . assertEqual ( i2i . _users [ <NUM_LIT:12> ] , set ( [ <NUM_LIT:100> ] ) ) <EOL> self . assertEqual ( i2i . similarity ( <NUM_LIT:100> , <NUM_LIT:100> ) , <NUM_LIT:1.0> ) <EOL> self . assertEqual ( i2i . similarity ( <NUM_LIT:100> , <NUM_LIT> ) , <NUM_LIT:1.0> / <NUM_LIT> ) <EOL> def test_score ( self ) : <EOL> df = pd . DataFrame ( [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> } , { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT> } , { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT:100> } ] ) <EOL> i2i = Item2Item ( ) <EOL> i2i . fit ( df ) <EOL> self . assertEqual ( i2i . get_score ( <NUM_LIT:12> , <NUM_LIT> ) , <NUM_LIT:0.5> ) <EOL> class TestLSI ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . lsi = LSIModel ( "<STR_LIT:title>" ) <EOL> self . df = pd . read_csv ( resource_filename ( testfm . __name__ , "<STR_LIT>" ) , sep = "<STR_LIT>" , header = None , <EOL> names = [ "<STR_LIT:user>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:date>" , "<STR_LIT:title>" ] ) <EOL> def test_fit ( self ) : <EOL> self . lsi . fit ( self . df ) <EOL> self . assertEqual ( len ( self . lsi . _user_representation ) , len ( self . df . user . unique ( ) ) ) <EOL> self . assertEqual ( len ( self . lsi . _item_representation ) , len ( self . df . item . unique ( ) ) ) <EOL> def test_score ( self ) : <EOL> self . lsi . fit ( self . df ) <EOL> self . assertTrue ( self . lsi . get_score ( <NUM_LIT:1> , <NUM_LIT> ) > self . lsi . get_score ( <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> def test_user_model ( self ) : <EOL> um = self . lsi . _get_user_models ( self . df ) <EOL> self . assertEqual ( um [ <NUM_LIT> ] , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_item_model ( self ) : <EOL> im = self . lsi . _get_item_models ( self . df ) <EOL> self . assertEqual ( im [ <NUM_LIT> ] , [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( im [ <NUM_LIT> ] , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> class TFIDTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . df = pd . DataFrame ( [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:100> , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : "<STR_LIT>" } ] ) <EOL> def test_item_user_model ( self ) : <EOL> tfidf = TFIDFModel ( "<STR_LIT>" ) <EOL> tfidf . fit ( self . df ) <EOL> self . assertEqual ( tfidf . _get_item_models ( self . df ) , { <NUM_LIT:1> : [ "<STR_LIT>" , "<STR_LIT>" ] , <NUM_LIT:100> : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> <NUM_LIT> : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] } ) <EOL> self . assertEqual ( tfidf . _users , { <NUM_LIT:10> : set ( [ <NUM_LIT:100> ] ) , <NUM_LIT:11> : set ( [ <NUM_LIT:100> , <NUM_LIT:1> ] ) , <NUM_LIT:12> : set ( [ <NUM_LIT> ] ) } ) <EOL> self . assertEqual ( sorted ( tfidf . tfidf . keys ( ) ) , sorted ( [ <NUM_LIT:100> , <NUM_LIT:1> , <NUM_LIT> ] ) ) <EOL> def test_item_model ( self ) : <EOL> tfidf = TFIDFModel ( "<STR_LIT>" ) <EOL> tfidf . fit ( self . df ) <EOL> self . assertAlmostEqual ( tfidf . _sim ( <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT:1> , places = <NUM_LIT:2> ) <EOL> self . assertAlmostEqual ( tfidf . _sim ( <NUM_LIT:100> , <NUM_LIT:100> ) , <NUM_LIT:1> , places = <NUM_LIT:2> ) <EOL> self . assertGreater ( tfidf . _sim ( <NUM_LIT:1> , <NUM_LIT:1> ) , tfidf . _sim ( <NUM_LIT:1> , <NUM_LIT:100> ) , "<STR_LIT>" ) <EOL> def test_get_score ( self ) : <EOL> tfidf = TFIDFModel ( "<STR_LIT>" ) <EOL> tfidf . fit ( self . df ) <EOL> tfidf . k = <NUM_LIT:1> <EOL> self . assertAlmostEqual ( tfidf . get_score ( <NUM_LIT:10> , <NUM_LIT:1> ) , tfidf . _sim ( <NUM_LIT:100> , <NUM_LIT:1> ) , places = <NUM_LIT:2> ) <EOL> class SVDppTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . df = pd . DataFrame ( [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> , "<STR_LIT>" : <NUM_LIT:5> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:100> , "<STR_LIT>" : <NUM_LIT:4> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:3> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT:2> } ] ) <EOL> self . df_big = pd . read_csv ( resource_filename ( testfm . __name__ , "<STR_LIT>" ) , <EOL> sep = "<STR_LIT>" , header = None , names = [ "<STR_LIT:user>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:date>" , "<STR_LIT:title>" ] ) <EOL> @ unittest . skipIf ( not which ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def test_train ( self ) : <EOL> svdpp = SVDpp ( ) <EOL> self . assertFalse ( hasattr ( svdpp , "<STR_LIT>" ) ) <EOL> svdpp . fit ( self . df_big ) <EOL> self . assertTrue ( hasattr ( svdpp , "<STR_LIT>" ) ) <EOL> self . assertEqual ( svdpp . U . shape , ( len ( self . df_big . user . unique ( ) ) , <NUM_LIT:20> ) ) <EOL> self . assertEqual ( svdpp . V . shape , ( len ( self . df_big . item . unique ( ) ) , <NUM_LIT:20> ) ) <EOL> self . assertEqual ( svdpp . U_bias . shape , ( len ( self . df_big . user . unique ( ) ) , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( svdpp . V_bias . shape , ( len ( self . df_big . item . unique ( ) ) , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( svdpp . global_mean . shape , ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> @ unittest . skipIf ( not which ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> def test_score ( self ) : <EOL> import types <EOL> svdpp = SVDpp ( ) <EOL> svdpp . fit ( self . df ) <EOL> self . assertTrue ( isinstance ( svdpp . get_score ( <NUM_LIT:10> , <NUM_LIT:100> ) , types . FloatType ) ) <EOL> self . assertTrue ( isinstance ( svdpp . get_score ( <NUM_LIT:12> , <NUM_LIT> ) , types . FloatType ) ) <EOL> def test_dump ( self ) : <EOL> svdpp = SVDpp ( ) <EOL> filename = svdpp . dump_data ( self . df ) <EOL> lines = open ( filename ) . readlines ( ) <EOL> self . assertEqual ( lines [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( lines [ <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( lines [ <NUM_LIT:3> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( lines [ <NUM_LIT:4> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( svdpp . umap ) ) <EOL> self . assertEqual ( svdpp . umap [ <NUM_LIT:10> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( svdpp . umap [ <NUM_LIT:11> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( svdpp . umap [ <NUM_LIT:12> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( svdpp . imap ) ) <EOL> self . assertEqual ( svdpp . imap [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( svdpp . imap [ <NUM_LIT:100> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( svdpp . imap [ <NUM_LIT> ] , <NUM_LIT:2> ) <EOL> class MeanPredTest ( unittest . TestCase ) : <EOL> df = pd . DataFrame ( [ { "<STR_LIT:user>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:100> , "<STR_LIT>" : <NUM_LIT:5> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:100> , "<STR_LIT>" : <NUM_LIT:4> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:3> } , <EOL> { "<STR_LIT:user>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT:2> } ] ) <EOL> def test_fit ( self ) : <EOL> model = AverageModel ( ) <EOL> model . fit ( self . df ) <EOL> self . assertEqual ( model . get_score ( <NUM_LIT:10> , <NUM_LIT:100> ) , <NUM_LIT> ) <EOL> class PyTensorTest ( object ) : <EOL> def test_user_model_update ( self ) : <EOL> pyTF = PyTensorCoFi ( ) <EOL> Y = np . array ( [ [ - <NUM_LIT> , - <NUM_LIT> ] , [ - <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , - <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , - <NUM_LIT> ] , [ - <NUM_LIT> , - <NUM_LIT> ] ] ) <EOL> user_items = [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> res = pyTF . online_user_factors ( Y , user_items , p_param = <NUM_LIT:10> , lambda_param = <NUM_LIT> ) <EOL> assert np . array ( [ - <NUM_LIT> , - <NUM_LIT> ] ) . all ( ) == res . all ( ) , "<STR_LIT>" <EOL> def test_dynamic_updates ( self ) : <EOL> """<STR_LIT>""" <EOL> pyTF = PyTensorCoFi ( n_factors = <NUM_LIT:20> , n_iterations = <NUM_LIT:5> , c_lambda = <NUM_LIT> , c_alpha = <NUM_LIT> ) <EOL> evaluator = Evaluator ( ) <EOL> tf = TensorCoFi ( n_factors = <NUM_LIT:2> , n_iterations = <NUM_LIT:100> , c_lambda = <NUM_LIT> , c_alpha = <NUM_LIT> ) <EOL> df = pd . read_csv ( resource_filename ( testfm . __name__ , "<STR_LIT>" ) , sep = "<STR_LIT>" , header = None , <EOL> names = [ "<STR_LIT:user>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:date>" , "<STR_LIT:title>" ] ) <EOL> training , testing = testfm . split . holdoutByRandom ( df , <NUM_LIT> ) <EOL> users = { user : list ( entries ) for user , entries in training . groupby ( "<STR_LIT:user>" ) [ "<STR_LIT>" ] } <EOL> tf . fit ( training ) <EOL> map1 = evaluator . evaluate_model ( tf , testing ) <EOL> for u , items in users . items ( ) : <EOL> uid = tf . data_map [ tf . get_user_column ( ) ] [ u ] <EOL> iids = [ tf . data_map [ tf . get_item_column ( ) ] [ i ] for i in items ] <EOL> new_factors = pyTF . online_user_factors ( tf . factors [ <NUM_LIT:1> ] , iids , p_param = <NUM_LIT> , lambda_param = <NUM_LIT> ) <EOL> tf . factors [ <NUM_LIT:0> ] [ uid , : ] = new_factors <EOL> map2 = evaluator . evaluate_model ( tf , testing ) <EOL> assert abs ( map1 [ <NUM_LIT:0> ] - map2 [ <NUM_LIT:0> ] ) < <NUM_LIT> * map1 [ <NUM_LIT:0> ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
