Ejemplo n.º 1
0
 def __init__(self, grammar, forward_model=None, data=None, ll_params=None, spatial_model=None, initial_tree=None):
     """
     Initializes ShapeGrammarState
     grammar: Probabilistic context free shape grammar definition. PCFG instance.
     forward_model: Forward model(s) used in likelihood calculation
     ll_params: parameters for likelihood calculation
     spatial_model: Spatial model for the state. Initialized randomly if initial_tree is not
                     provided
     initial_tree (optional): Parse tree for the state. If not provided, state is initialized
                     randomly.
     """
     self.grammar = grammar
     self.spatial_model = spatial_model
     self.forward_model = forward_model
         
     if initial_tree is None:
         self.tree = self._get_random_tree(start=self.grammar.start_symbol, max_depth=self.MAXIMUM_DEPTH)
         # initialize spatial model
         self.spatial_model.update(self.tree, self.grammar)
     else:
         self.tree = initial_tree
         
     # if a subclass does not define moves, we define it here and add subtree move
     if hasattr(self, 'moves') is False:
         self.moves = [self.subtree_proposal]
         
         
     # IMPORTANT: we call base class init after we initialize spatial model, 
     # because prior, ll, deriv_prob calculation is done in init and
     # spatial_model should be available to calculate them
     PCFGTree.__init__(self, grammar=grammar, data=data, ll_params=ll_params, initial_tree=self.tree)
    def __init__(self,
                 grammar,
                 forward_model=None,
                 data=None,
                 ll_params=None,
                 spatial_model=None,
                 initial_tree=None):
        """
        Initializes ShapeGrammarState
        grammar: Probabilistic context free shape grammar definition. PCFG instance.
        forward_model: Forward model(s) used in likelihood calculation
        ll_params: parameters for likelihood calculation
        spatial_model: Spatial model for the state. Initialized randomly if initial_tree is not
                        provided
        initial_tree (optional): Parse tree for the state. If not provided, state is initialized
                        randomly.
        """
        self.grammar = grammar
        self.spatial_model = spatial_model
        self.forward_model = forward_model

        if initial_tree is None:
            self.tree = self._get_random_tree(start=self.grammar.start_symbol,
                                              max_depth=self.MAXIMUM_DEPTH)
            # initialize spatial model
            self.spatial_model.update(self.tree, self.grammar)
        else:
            self.tree = initial_tree

        # if a subclass does not define moves, we define it here and add subtree move
        if hasattr(self, 'moves') is False:
            self.moves = [self.subtree_proposal]

        # IMPORTANT: we call base class init after we initialize spatial model,
        # because prior, ll, deriv_prob calculation is done in init and
        # spatial_model should be available to calculate them
        PCFGTree.__init__(self,
                          grammar=grammar,
                          data=data,
                          ll_params=ll_params,
                          initial_tree=self.tree)