def optimize_branch_length(self, mode='<STR_LIT>', **kwargs):
self.logger("<STR_LIT>"%mode,<NUM_LIT:1>)<EOL>if (self.tree is None) or (self.aln is None):<EOL><INDENT>self.logger("<STR_LIT>", <NUM_LIT:0>)<EOL>return ttconf.ERROR<EOL><DEDENT>store_old_dist = False<EOL>if '<STR_LIT>' in kwargs:<EOL><INDENT>store_old_dist = kwargs['<STR_LIT>']<EOL><DEDENT>if mode=='<STR_LIT>':<EOL><INDENT>if not hasattr(self.tree.root, "<STR_LIT>"):<EOL><INDENT>self.infer_ancestral_sequences(marginal=True)<EOL><DEDENT><DEDENT>max_bl = <NUM_LIT:0><EOL>for node in self.tree.find_clades(order='<STR_LIT>'):<EOL><INDENT>if node.up is None: continue <EOL>if store_old_dist:<EOL><INDENT>node._old_length = node.branch_length<EOL><DEDENT>if mode=='<STR_LIT>':<EOL><INDENT>new_len = self.optimal_marginal_branch_length(node)<EOL><DEDENT>elif mode=='<STR_LIT>':<EOL><INDENT>new_len = self.optimal_branch_length(node)<EOL><DEDENT>else:<EOL><INDENT>self.logger("<STR_LIT>",<NUM_LIT:4>, warn=True)<EOL>new_len = node.branch_length<EOL><DEDENT>if new_len < <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>self.logger("<STR_LIT>"<EOL>"<STR_LIT>"%(node.branch_length, new_len, len(node.mutations)*self.one_mutation), <NUM_LIT:5>)<EOL>node.branch_length = new_len<EOL>node.mutation_length=new_len<EOL>max_bl = max(max_bl, new_len)<EOL><DEDENT>self.tree.root.up = None<EOL>self.tree.root.dist2root = <NUM_LIT:0.0><EOL>if max_bl><NUM_LIT> and mode=='<STR_LIT>':<EOL><INDENT>self.logger("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>", <NUM_LIT:0>, warn=True)<EOL><DEDENT>self._prepare_nodes()<EOL>return ttconf.SUCCESS<EOL>
Perform optimization for the branch lengths of the entire tree.
This method only does a single path and needs to be iterated.

**Note** this method assumes that each node stores information
about its sequence as numpy.array object (node.sequence attribute).
Therefore, before calling this method, sequence reconstruction with
either of the available models must be performed.

Parameters
----------

 mode : str
    Optimize branch length assuming the joint ML sequence assignment
    of both ends of the branch (:code:`joint`), or trace over all possible sequence
    assignments on both ends of the branch (:code:`marginal`) (slower, experimental).

 **kwargs :
    Keyword arguments

Keyword Args
------------

 verbose : int
    Output level

 store_old : bool
    If True, the old lengths will be saved in :code:`node._old_dist` attribute.
    Useful for testing, and special post-processing.