Ejemplo n.º 1
0
    def __init__(self, grammar=None, value=None, f=None, maxnodes=25, args=['x'], **kwargs):

        # Save all of our keywords
        self.__dict__.update(locals())

        if value is None and grammar is not None:
            value = grammar.generate()

        FunctionHypothesis.__init__(self, value=value, f=f, args=args, **kwargs)

        self.likelihood = 0.0
        self.rules_vector = None
Ejemplo n.º 2
0
    def __init__(self, grammar=None, value=None, f=None, maxnodes=25, **kwargs):

        if 'args' in kwargs:
            assert False, "*** Use of 'args' is deprecated. Use display='...' instead."

        # Save all of our keywords
        self_update(self, locals())
        if value is None and grammar is not None:
            value = grammar.generate()

        FunctionHypothesis.__init__(self, value=value, f=f, **kwargs)

        self.likelihood = 0.0
        self.rules_vector = None
Ejemplo n.º 3
0
 def __call__(self, *args):
     # NOTE: This no longer catches all exceptions.
     try:
         return FunctionHypothesis.__call__(self, *args)
     except TypeError as e:
         print "TypeError in function call: ", e, str(self), "  ;  ", type(self), args
         raise TypeError
     except NameError as e:
         print "NameError in function call: ", e, " ; ", str(self), args
         raise NameError
Ejemplo n.º 4
0
    def force_function(self, w, f):
        """
            Allow force_function
        """

        # If this does not exist, make a function hypothesis from scratch with nothing in it.
        if w not in self.value:
            self.value[w] = FunctionHypothesis(value=None, args=None)

        self.value[w].force_function(f)
Ejemplo n.º 5
0
    def __init__(self,
                 grammar=None,
                 value=None,
                 f=None,
                 maxnodes=25,
                 **kwargs):

        if 'args' in kwargs:
            assert False, "*** Use of 'args' is deprecated. Use display='...' instead."

        # Save all of our keywords
        self_update(self, locals())
        if value is None and grammar is not None:
            value = grammar.generate()

        FunctionHypothesis.__init__(self, value=value, f=f, **kwargs)

        self.likelihood = 0.0
        self.rules_vector = None
Ejemplo n.º 6
0
 def __call__(self, *args):
     # NOTE: This no longer catches all exceptions.
     try:
         return FunctionHypothesis.__call__(self, *args)
     except TypeError as e:
         print "TypeError in function call: ", e, str(self), "  ;  ", type(
             self), args
         raise TypeError
     except NameError as e:
         print "NameError in function call: ", e, " ; ", str(self), args
         raise NameError
Ejemplo n.º 7
0
    def recursive_call(self, *args):
        """
        This gets called internally on recursive calls. It keeps track of the depth and throws an error if you go too deep
        """

        self.recursive_call_depth += 1

        if self.recursive_call_depth > self.recursive_depth_bound:
            raise RecursionDepthException

        # Call with sending myself as the recursive call
        return FunctionHypothesis.__call__(self, self.recursive_call, *args)
Ejemplo n.º 8
0
    def __init__(self,
                 grammar,
                 value=None,
                 f=None,
                 node_counts=None,
                 maxnodes=25,
                 recurse_bound=25,
                 display="lambda recurse_, x: %s",
                 **kwargs):
        """
        Initializer. recurse gives the name for the recursion operation internally.
        """
        assert "lambda recurse_" in display, "*** RecursiveLOTHypothesis must have 'recurse_' as first display element."  # otherwise it can't eval

        # save recurse symbol
        self.recursive_depth_bound = recurse_bound  # how deep can we recurse?
        self.recursive_call_depth = 0  # how far down have we recursed?

        if 'args' in kwargs:
            assert False, "*** Use of 'args' is deprecated. Use display='...' instead."

        # Save all of our keywords
        self_update(self, locals())
        grammar.update_alphas()  # make sure alpha/sigma arrays are initialized
        if value is None:
            value, self.node_counts = grammar.generate_with_counts()
        else:
            self.node_counts = node_counts
        self.tree_size = np.sum(self.node_counts)

        FunctionHypothesis.__init__(self,
                                    value=value,
                                    f=f,
                                    display=display,
                                    **kwargs)

        self.likelihood = 0.0
        self.compute_prior()
        self.rules_vector = None
Ejemplo n.º 9
0
    def __init__(self,
                 grammar=None,
                 value=None,
                 f=None,
                 maxnodes=25,
                 args=['x'],
                 **kwargs):

        # Save all of our keywords
        self.__dict__.update(locals())

        if value is None and grammar is not None:
            value = grammar.generate()

        FunctionHypothesis.__init__(self,
                                    value=value,
                                    f=f,
                                    args=args,
                                    **kwargs)

        self.likelihood = 0.0
        self.rules_vector = None
Ejemplo n.º 10
0
    def __init__(self, grammar, value=None, f=None, start=None, ALPHA=0.9, maxnodes=25, args=['x'],
                 proposal_function=None, **kwargs):
        self.grammar = grammar
        self.f = f
        self.ALPHA = ALPHA
        self.maxnodes = maxnodes

        # Save all of our keywords (though we don't need v)
        self.__dict__.update(locals())

        # If this is not specified, defaultly use grammar
        if start is None:
            self.start = grammar.start
        if value is None:
            value = grammar.generate(self.start)

        FunctionHypothesis.__init__(self, value=value, f=f, args=args, **kwargs)
        # Save a proposal function
        # TODO: How to handle this in copying?
        if proposal_function is None:
            self.proposal_function = RegenerationProposal(self.grammar)

        self.likelihood = 0.0
        self.rules_vector = None
Ejemplo n.º 11
0
    def __call__(self, *args):
        """
        The main calling function. Resets recursive_call_depth and then calls
        """
        self.recursive_call_depth = 0

        try:
            # call with passing self.recursive_Call as the recursive call
            return FunctionHypothesis.__call__(self, self.recursive_call,
                                               *args)
        except TypeError as e:
            print "TypeError in function call: ", e, str(self), "  ;  ", type(
                self), args
            raise TypeError
        except NameError as e:
            print "NameError in function call: ", e, " ; ", str(self), args
            raise NameError