def handle_dry(self, options):
     info = self.load_environment_info(options['initial'], options['config_name'], True)
     environment = InMemoryEnvironment()
     environment = self.load_environment(info)
     users, items = self.load_user_and_item_ids(info, options['batch_size'])
     items += list(set(flatten(Item.objects.get_reachable_parents(items).values())))
     environment.prefetch(users, items)
     predictive_model = get_predictive_model(info.to_json())
     with closing(connection.cursor()) as cursor:
         cursor.execute('SELECT COUNT(*) FROM proso_models_answer')
         answers_total = cursor.fetchone()[0]
         if options['limit'] is not None:
             answers_total = min(answers_total, options['limit'])
         print('total:', answers_total)
         processed = 0
         prediction = numpy.empty(answers_total)
         correct = numpy.empty(answers_total)
         while processed < answers_total:
             cursor.execute(
                 '''
                 SELECT
                     id,
                     user_id,
                     item_id,
                     item_asked_id,
                     item_answered_id,
                     time,
                     response_time,
                     guess
                 FROM proso_models_answer
                 ORDER BY id
                 OFFSET %s LIMIT %s
                 ''', [processed, options['batch_size']])
             for (answer_id, user, item, asked, answered, time, response_time, guess) in cursor:
                 correct[processed] = asked == answered
                 prediction[processed] = predictive_model.predict_and_update(
                     environment,
                     user,
                     item,
                     asked == answered,
                     time,
                     item_answered=answered,
                     item_asked=asked,
                     guess=guess,
                     answer_id=answer_id,
                     response_time=response_time,
                 )
                 environment.process_answer(user, item, asked, answered, time, answer_id, response_time, guess)
                 processed += 1
                 if processed >= answers_total:
                     break
             print('processed:', processed)
     filename = settings.DATA_DIR + '/recompute_model_report_{}.json'.format(predictive_model.__class__.__name__)
     model_report = report(prediction, correct)
     with open(filename, 'w') as outfile:
         json.dump(model_report, outfile)
     print('Saving report to:', filename)
     brier_graphs(model_report['brier'], predictive_model)
Пример #2
0
 def write(self, key, value, user=None, item=None, item_secondary=None, time=None, symmetric=True, permanent=False, answer=None):
     prefetched_key = self._prefetched_key(key, user, item, item_secondary, symmetric)
     prefetched = self._prefetched.get(prefetched_key)
     if prefetched is not None:
         self._to_delete.append(prefetched[2])
         del self._prefetched[prefetched_key]
     InMemoryEnvironment.write(
         self, key, value, user=user, item=item,
         item_secondary=item_secondary, time=time,
         symmetric=symmetric, permanent=permanent, answer=answer
     )
Пример #3
0
 def write(self, key, value, user=None, item=None, item_secondary=None, time=None, audit=True, symmetric=True, permanent=False, answer=None):
     prefetched_key = self._prefetched_key(key, user, item, item_secondary, symmetric)
     prefetched = self._prefetched.get(prefetched_key)
     if prefetched is not None:
         self._to_delete.append(prefetched[2])
         del self._prefetched[prefetched_key]
     InMemoryEnvironment.write(
         self, key, value, user=user, item=item,
         item_secondary=item_secondary, time=time, audit=audit,
         symmetric=symmetric, permanent=permanent, answer=answer
     )
Пример #4
0
 def time(self, key, user=None, item=None, item_secondary=None, symmetric=True):
     prefetched = self._get_prefetched(key, user, item, item_secondary, symmetric)
     if prefetched:
         return prefetched[0]
     else:
         return InMemoryEnvironment.time(
             self, key, user=user, item=item,
             item_secondary=item_secondary, symmetric=symmetric
         )
Пример #5
0
 def read(self, key, user=None, item=None, item_secondary=None, default=None, symmetric=True):
     prefetched = self._get_prefetched(key, user, item, item_secondary, symmetric)
     if prefetched:
         return prefetched[1]
     else:
         return InMemoryEnvironment.read(
             self, key, user=user, item=item, item_secondary=item_secondary,
             default=default, symmetric=symmetric
         )
Пример #6
0
 def time(self, key, user=None, item=None, item_secondary=None, symmetric=True):
     prefetched = self._get_prefetched(key, user, item, item_secondary, symmetric)
     if prefetched:
         return prefetched[0]
     else:
         return InMemoryEnvironment.time(
             self, key, user=user, item=item,
             item_secondary=item_secondary, symmetric=symmetric
         )
Пример #7
0
 def read(self, key, user=None, item=None, item_secondary=None, default=None, symmetric=True):
     prefetched = self._get_prefetched(key, user, item, item_secondary, symmetric)
     if prefetched:
         return prefetched[1]
     else:
         return InMemoryEnvironment.read(
             self, key, user=user, item=item, item_secondary=item_secondary,
             default=default, symmetric=symmetric
         )
Пример #8
0
 def read_all_with_key(self, key):
     found = []
     for k, v in self._prefetched.items():
         if k[0] == key:
             found.append((k[1], k[2], k[3], v[1]))
     return found + InMemoryEnvironment.read_all_with_key(self, key)
Пример #9
0
 def __init__(self, info):
     # key -> user -> item_primary -> item_secondary -> [(time, value)]
     InMemoryEnvironment.__init__(self)
     self._prefetched = {}
     self._info_id = info.id
     self._to_delete = []
Пример #10
0
 def __init__(self, info):
     # key -> user -> item_primary -> item_secondary -> [(time, value)]
     InMemoryEnvironment.__init__(self)
     self._prefetched = {}
     self._info_id = info.id
     self._to_delete = []
Пример #11
0
 def handle_dry(self, options):
     info = self.load_environment_info(options['initial'],
                                       options['config_name'], True)
     environment = InMemoryEnvironment(audit_enabled=False)
     environment = self.load_environment(info)
     users, items = self.load_user_and_item_ids(info, options['batch_size'])
     items += list(
         set(flatten(Item.objects.get_reachable_parents(items).values())))
     environment.prefetch(users, items)
     predictive_model = get_predictive_model(info.to_json())
     with closing(connection.cursor()) as cursor:
         cursor.execute('SELECT COUNT(*) FROM proso_models_answer')
         answers_total = cursor.fetchone()[0]
         if options['limit'] is not None:
             answers_total = min(answers_total, options['limit'])
         print('total:', answers_total)
         processed = 0
         prediction = numpy.empty(answers_total)
         correct = numpy.empty(answers_total)
         while processed < answers_total:
             cursor.execute(
                 '''
                 SELECT
                     id,
                     user_id,
                     item_id,
                     item_asked_id,
                     item_answered_id,
                     time,
                     response_time,
                     guess
                 FROM proso_models_answer
                 ORDER BY id
                 OFFSET %s LIMIT %s
                 ''', [processed, options['batch_size']])
             for (answer_id, user, item, asked, answered, time,
                  response_time, guess) in cursor:
                 correct[processed] = asked == answered
                 prediction[
                     processed] = predictive_model.predict_and_update(
                         environment,
                         user,
                         item,
                         asked == answered,
                         time,
                         item_answered=answered,
                         item_asked=asked,
                         guess=guess,
                         answer_id=answer_id,
                         response_time=response_time,
                     )
                 environment.process_answer(user, item, asked, answered,
                                            time, answer_id, response_time,
                                            guess)
                 processed += 1
                 if processed >= answers_total:
                     break
             print('processed:', processed)
     filename = settings.DATA_DIR + '/recompute_model_report_{}.json'.format(
         predictive_model.__class__.__name__)
     model_report = report(prediction, correct)
     with open(filename, 'w') as outfile:
         json.dump(model_report, outfile)
     print('Saving report to:', filename)
     brier_graphs(model_report['brier'], predictive_model)