Exemplo n.º 1
0
 def save(self): 
     #replace old user model with new
     try:
         #replace profile
         UserModel.objects(user_id=self.user.id).update(upsert=True,
                                                        set__user_id=self.user.id,
                                                        set__data=self.user_model_features,
                                                        set__version=self.get_version())
     except Exception as inst:
         logger.error("Could not save learned user model due to unknown error %s: %s" % (type(inst), inst))
Exemplo n.º 2
0
 def save(self): 
     #replace old user model with new
     try:
         #replace profile
         #pickle classifier and decode it to utf-8
         #pickled_classifier = cPickle.dumps(self.user_model_features).decode('utf-8')
         UserModel.objects(user_id=self.user.id).update(upsert=True,
                                                        set__user_id=self.user.id,
                                                        set__data=self.user_model_features,
                                                        set__version=self.get_version())
     except Exception as inst:
         logger.error("Could not save learned user model due to unknown error %s: %s" % (type(inst), inst))
Exemplo n.º 3
0
 def load(self):
     try:
         if self.clf is not None:
             return
         
         user_model = UserModel.objects(user_id = self.user.id).first()
         
         if user_model is None:
             logger.debug("UserModel for user %s is empty." % self.user.id)
             self.clf = None
             return
         
         #ensure right version
         if user_model.version != self.get_version():
             logger.debug("UserModel for user %s has wrong version." %
                          self.user.id)
             self.clf = None
             return
         
         #unpickle classifier. it was saved as a utf-8 string.
         #get the str object by encoding it.
         pickled_classifier = user_model.data.clf.encode('utf-8')
         pickled_theta = user_model.data.theta.encode('utf-8')
         pickled_sigma = user_model.data.sigma.encode('utf-8')
         
         self.clf = cPickle.loads(pickled_classifier)
         self.theta_ = cPickle.loads(pickled_theta)
         self.sigma_ = cPickle.loads(pickled_sigma)
             
     except Exception as inst:
         logger.error("Could not load learned user model due to unknown"
                      " error %s: %s" % (type(inst), inst))
Exemplo n.º 4
0
    def load(self):
        """
        Loads user model from self.user. Not yet from UserModel
        
        NOTE: No feature conversion is done!
        """
        user_model = UserModel.objects(user_id=self.user.id).first()

        if user_model is None:
            logger.debug("UserModel for user %s is empty." % self.user.id)
            self.clf = None
            return
        
        #learned_user_model = UserModel.objects(user_id=self.user.id).first()
        #
        #if learned_user_model is None:
        #    self.user_model_features = []
        #    return
        #
        #get learned profile/model
        #convert features to list of tuples. 
        #we make a double list because we will have more than one model soon.
        #self.user_model_features = [[tuple(a) for a in learned_user_model.data] for profile in self.user.learned_profile]

        #unpickle classifier. it was saved as a utf-8 string.
        #get the str object by encoding it.
        #pickled_classifier = user_model.data.encode('utf-8')
        #self.clf = cPickle.loads(pickled_classifier)

        self.user_model_features = [tuple(a) for a in self.learned_user_model.data]
Exemplo n.º 5
0
 def test_save(self):
     self.trainer.train()
     self.trainer.save()
     
     user_model = UserModel.objects(user_id = self.user_id).first()
     
     self.assertAlmostEqual(user_model.data[0][1], 
                            0.0021, 4)    
Exemplo n.º 6
0
 def save(self):
     #replace old user model with new
     try:
         #pickle classifier and decode it to utf-8
         pickled_classifier = cPickle.dumps(self.clf).decode('utf-8')
         
         pickled_theta = cPickle.dumps(self.theta_).decode('utf-8')
         pickled_sigma = cPickle.dumps(self.sigma_).decode('utf-8')
         
         data = {'clf': pickled_classifier, 
                 'theta': pickled_theta,
                 'sigma': pickled_sigma}
         
         #replace profile
         UserModel.objects(user_id=self.user.id).update(upsert=True, set__user_id=self.user.id, set__data=data,
                                                        set__version=self.get_version())
         
     except Exception as inst:
         logger.error("Could not save learned user model due to unknown error %s: %s" % (type(inst), inst))
Exemplo n.º 7
0
    def __init__(self, user_id, extractor):
        super(UserModelCentroid, self).__init__(user_id, extractor)

        # instance vars
        self.user_model_features = []
        self.learned_user_model = UserModel.objects(user_id=self.user.id).first()