def test_validate_arg_3(self):
     i = {'instruction': "moo", 'symbol': 2}
     try:
         utils.validate_arg(i, 'red', utils.validate_symbol)
     except VentureException as e:
         self.assertEqual(e.exception, 'missing_argument')
         self.assertEqual(e.data['argument'], 'red')
示例#2
0
 def _do_labeled_predict(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              modifier=_modify_expression,
                              wrap_exception=False)
     label = utils.validate_arg(instruction, 'label', utils.validate_symbol)
     did, val = self.engine.labeled_predict(label, exp)
     return {'directive_id': did, 'value': val}
示例#3
0
 def _do_define(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              modifier=_modify_expression,
                              wrap_exception=False)
     sym = utils.validate_arg(instruction, 'symbol', utils.validate_symbol)
     (did, val) = self.engine.define(sym, exp)
     return {"directive_id": did, "value": val}
示例#4
0
 def _do_observe(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              modifier=_modify_expression,
                              wrap_exception=False)
     val = utils.validate_arg(instruction,
                              'value',
                              utils.validate_value,
                              modifier=_modify_value)
     did, weights = self.engine.observe(exp, val)
     return {"directive_id": did, "value": weights}
示例#5
0
 def _do_labeled_observe(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              modifier=_modify_expression,
                              wrap_exception=False)
     val = utils.validate_arg(instruction,
                              'value',
                              utils.validate_value,
                              modifier=_modify_value)
     label = utils.validate_arg(instruction, 'label', utils.validate_symbol)
     did, weights = self.engine.labeled_observe(label, exp, val)
     return {'directive_id': did, 'value': weights}
 def test_validate_arg_5(self):
     i = {'instruction': "moo", 'symbol': "moo"}
     self.assertEqual(
         utils.validate_arg(i,
                            'symbol',
                            utils.validate_symbol,
                            modifier=lambda x: 'red'), 'red')
示例#7
0
 def _do_start_continuous_inference(self, instruction):
     e = utils.validate_arg(instruction,
                            'expression',
                            utils.validate_expression,
                            modifier=_modify_expression,
                            wrap_exception=False)
     self.engine.start_continuous_inference(e)
示例#8
0
 def _do_predict_all(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              modifier=_modify_expression,
                              wrap_exception=False)
     did, val = self.engine.predict_all(exp)
     return {"directive_id": did, "value": val}
示例#9
0
 def _do_infer(self, instruction):
     e = utils.validate_arg(instruction,
                            'expression',
                            utils.validate_expression,
                            modifier=_modify_expression,
                            wrap_exception=False)
     (did, val) = self.engine.infer(e)
     return {"directive_id": did, "value": val}
示例#10
0
 def _do_force(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              wrap_exception=False)
     val = utils.validate_arg(instruction, 'value', utils.validate_value)
     inst1 = {
         'instruction': 'observe',
         'expression': exp,
         'value': val,
     }
     o1 = self._call_core_sivm_instruction(inst1)
     inst2 = {
         'instruction': 'infer',
         'expression': [v.symbol('incorporate')]
     }
     self._call_core_sivm_instruction(inst2)
     inst3 = {
         'instruction': 'forget',
         'directive_id': o1['directive_id'],
     }
     self._call_core_sivm_instruction(inst3)
     return {"value": o1["value"]}
示例#11
0
 def _do_sample_all(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              wrap_exception=False)
     inst1 = {
         'instruction': 'predict_all',
         'expression': exp,
     }
     o1 = self._call_core_sivm_instruction(inst1)
     inst2 = {
         'instruction': 'forget',
         'directive_id': o1['directive_id'],
     }
     self._call_core_sivm_instruction(inst2)
     return {'value': o1['value']}
示例#12
0
 def _register_executed_instruction(self, instruction, predicted_did,
                                    forgotten_did, response):
     if response is not None and 'directive_id' in response:
         if response['directive_id'] != predicted_did:
             warning = "Warning: Instruction %s was pre-assigned did %s but actually assigned did %s"
             print warning % (instruction, predicted_did,
                              response['directive_id'])
     elif predicted_did is not None:
         warning = "Warning: Instruction %s was pre-assigned did %s but not actually assigned any did"
         print warning % (instruction, predicted_did)
     instruction_type = instruction['instruction']
     # clear the dicts on the "clear" command
     if instruction_type == 'clear':
         self._clear()
     # forget directive mappings on the "forget" command
     if forgotten_did is not None:
         if forgotten_did in self.syntax_dict:
             del self.syntax_dict[forgotten_did]
         else:
             # XXX Presume that this is a fork-model directive id
             # collision as reported in Issue #586.
             pass
     if instruction_type in ['evaluate', 'infer']:
         # "evaluate" and "infer" are forgotten by the Engine;
         # forget them here, too.
         exp = utils.validate_arg(instruction,
                                  'expression',
                                  utils.validate_expression,
                                  wrap_exception=False)
         if instruction_type is 'infer' and self.core_sivm.engine.is_infer_loop_program(
                 exp):
             # We didn't save the infer loop thing
             pass
         else:
             # There is at least one way in which the predicted_did
             # may fail to be present in these dicts: if the
             # instruction being executed caused a "load" operation
             # (which mutates the current sivm!?).
             if predicted_did in self.syntax_dict:
                 del self.syntax_dict[predicted_did]
示例#13
0
 def _do_labeled_forget(self, instruction):
     label = utils.validate_arg(instruction, 'label', utils.validate_symbol)
     weights = self.engine.labeled_forget(label)
     return {'value': weights}
示例#14
0
 def test_validate_arg_1(self):
     i = {'instruction': "moo", 'symbol': "moo"}
     self.assertEqual(
         utils.validate_arg(i, 'symbol', utils.validate_symbol), "moo")
示例#15
0
 def _do_labeled_report(self, instruction):
     label = utils.validate_arg(instruction, 'label', utils.validate_symbol)
     value = self.engine.labeled_report_value(label)
     return {'value': value}
示例#16
0
 def _do_report(self, instruction):
     did = utils.validate_arg(instruction, 'directive_id',
                              utils.validate_nonnegative_integer)
     return {"value": self.engine.report_value(did)}
示例#17
0
 def _do_labeled_freeze(self, instruction):
     label = utils.validate_arg(instruction, 'label', utils.validate_symbol)
     self.engine.labeled_freeze(label)
     return {}
示例#18
0
 def test_validate_arg_4(self):
     i = {'instruction': "moo"}
     self.assertEqual(
         utils.validate_arg(i, 'red', utils.validate_symbol,
                            required=False), None)
示例#19
0
 def _do_forget(self, instruction):
     did = utils.validate_arg(instruction, 'directive_id',
                              utils.validate_nonnegative_integer)
     weights = self.engine.forget(did)
     return {"value": weights}
示例#20
0
 def _do_freeze(self, instruction):
     did = utils.validate_arg(instruction, 'directive_id',
                              utils.validate_nonnegative_integer)
     self.engine.freeze(did)
     return {}
示例#21
0
 def _call_core_sivm_instruction(self, instruction):
     desugared_instruction = copy.copy(instruction)
     instruction_type = instruction['instruction']
     predicted_did = None
     # desugar the expression
     if instruction_type in [
             'assume',
             'define',
             'evaluate',
             'infer',
             'labeled_assume',
             'labeled_observe',
             'labeled_predict',
             'observe',
             'predict',
             'predict_all',
     ]:
         exp = utils.validate_arg(instruction,
                                  'expression',
                                  utils.validate_expression,
                                  wrap_exception=False)
         syntax = macro_system.expand(exp)
         desugared_instruction['expression'] = syntax.desugared()
         # for error handling
         predicted_did = self._record_running_instruction(
             instruction, (exp, syntax))
     if instruction_type == 'forget':
         forgotten_did = instruction['directive_id']
     elif instruction_type == 'labeled_forget':
         label = utils.validate_arg(instruction, 'label',
                                    utils.validate_symbol)
         forgotten_did = self.core_sivm.engine.get_directive_id(label)
     else:
         forgotten_did = None
     try:
         response = self.core_sivm.execute_instruction(
             desugared_instruction)
     except VentureException as e:
         if self._do_not_annotate:
             raise
         import sys
         info = sys.exc_info()
         try:
             e = self._annotate(e, instruction)
         except Exception:
             print "Trying to annotate an exception at SIVM level led to:"
             import traceback
             print traceback.format_exc()
             raise e, None, info[2]
         finally:
             if instruction_type in [
                     'define', 'assume', 'observe', 'predict',
                     'predict_all', 'evaluate', 'infer'
             ]:
                 # After annotation completes, clear the syntax
                 # dictionary, because the instruction was
                 # (presumably!) not recorded in the underlying
                 # engine (so e.g. future list_directives commands
                 # should not list it)
                 if predicted_did in self.syntax_dict:
                     del self.syntax_dict[predicted_did]
         raise e, None, info[2]
     self._register_executed_instruction(instruction, predicted_did,
                                         forgotten_did, response)
     return response