<s> import sys <EOL> import numpy as np <EOL> import monk . core . api as monkapi <EOL> from monk . roles . configuration import get_config <EOL> from monk . roles . administrator import AdminBroker <EOL> from monk . roles . worker import WorkerBroker <EOL> from monk . roles . monitor import MonitorBroker <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def readline ( f ) : <EOL> line = f . readline ( ) <EOL> while line . strip ( ) == '<STR_LIT>' : <EOL> line = f . readline ( ) <EOL> return line <EOL> def readVector ( f ) : <EOL> size = int ( readline ( f ) ) <EOL> v = np . zeros ( size ) <EOL> for i in range ( size ) : <EOL> v [ i ] = float ( readline ( f ) ) <EOL> return v <EOL> def readMatrix ( f ) : <EOL> ns = readline ( f ) . split ( ) <EOL> nr = int ( ns [ <NUM_LIT:0> ] ) <EOL> nc = int ( ns [ <NUM_LIT:1> ] ) <EOL> m = np . zeros ( ( nr , nc ) ) <EOL> for i in range ( nr * nc ) : <EOL> fs = readline ( f ) . split ( ) <EOL> r = int ( fs [ <NUM_LIT:0> ] ) <EOL> c = int ( fs [ <NUM_LIT:1> ] ) <EOL> v = float ( fs [ <NUM_LIT:2> ] ) <EOL> m [ r , c ] = v <EOL> return m <EOL> def readModel ( filename ) : <EOL> userFactors = None <EOL> itemFactors = None <EOL> with open ( filename ) as f : <EOL> modelname = readline ( f ) <EOL> version = readline ( f ) <EOL> globalbias = float ( readline ( f ) ) <EOL> minRate = int ( readline ( f ) ) <EOL> maxRate = int ( readline ( f ) ) <EOL> userBias = readVector ( f ) <EOL> userFactors = readMatrix ( f ) <EOL> itemBias = readVector ( f ) <EOL> itemFactors = readMatrix ( f ) <EOL> return userFactors , itemFactors <EOL> def addUser ( ab , num = <NUM_LIT> , nameStub = '<STR_LIT>' ) : <EOL> ab . reconnect ( ) <EOL> for i in range ( num ) : <EOL> ab . add_user ( '<STR_LIT>' . format ( nameStub , i ) ) <EOL> def cloneMovieLensTurtle ( wb , master = '<STR_LIT>' , turtleName = '<STR_LIT>' , num = <NUM_LIT> , nameStub = '<STR_LIT>' ) : <EOL> wb . reconnect ( ) <EOL> for i in range ( num ) : <EOL> wb . add_clone ( master , turtleName , nameStub + str ( i ) ) <EOL> def addRatingData ( filename , itemFactors , turtleName , wb ) : <EOL> with open ( filename ) as f : <EOL> sts = readline ( f ) . split ( ) <EOL> userid = int ( sts [ <NUM_LIT:0> ] ) <EOL> itemid = int ( sts [ <NUM_LIT:1> ] ) <EOL> rating = int ( sts [ <NUM_LIT:2> ] ) <EOL> print userid , itemid , rating <EOL> def initialize ( ) : <EOL> config = get_config ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ab = AdminBroker ( config . kafkaConnectionString , config . administratorGroup , config . administratorTopic , producerType = AdminBroker . FIXED_PRODUCER , producerPartitions = [ <NUM_LIT:0> ] ) <EOL> wb = WorkerBroker ( config . kafkaConnectionString , config . workerGroup , config . workerTopic , producerType = WorkerBroker . USER_PRODUCER ) <EOL> mb = MonitorBroker ( config . kafkaConnectionString , config . monitorGroup , config . monitorTopic , producerType = MonitorBroker . FIXED_PRODUCER , producerPartitions = [ <NUM_LIT:0> ] ) <EOL> monkapi . exits ( ) <EOL> monkapi . initialize ( config ) <EOL> return ab , wb , mb <EOL> def train ( wb , nIter , master = '<STR_LIT>' , turtleName = '<STR_LIT>' , num = <NUM_LIT> , nameStub = '<STR_LIT>' ) : <EOL> wb . reconnect ( ) <EOL> for j in range ( nIter ) : <EOL> for i in range ( num ) : <EOL> user = nameStub + str ( i ) <EOL> wb . train ( user , turtleName ) <EOL> wb . merge ( master , turtleName , user ) <EOL> logger . info ( '<STR_LIT>' . format ( i ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> ab , wb , mb = initialize ( ) <EOL> userFactors , itemFactors = readModel ( sys . argv [ <NUM_LIT:1> ] ) <EOL> cloneMovieLensTurtle ( wb ) </s>
