# Path: treeCl/tasks.py
# def _fast_geo(tree1, tree2, normalise=False):
#     return getGeodesicDistance(tree1, tree2, normalise)
# 
# Path: treeCl/constants.py
# ISPY3 = sys.version_info.major>=3


import numpy as np
from scipy.spatial.distance import pdist, squareform
from .tasks import _fast_geo
from .constants import ISPY3
from tree_distance import PhyloTree
    x_new = x
    x_old = x-1 # dummy value
    while np.abs(x_new - x_old).sum() > tolerance:
        x_old = x_new
        step_size = golden_section_search(lambda step: f(grad_desc_update(x_old, a, c, step), a, c), -1.0, 1.0)
        x_new = grad_desc_update(x_old, a, c, step_size)
    return x_new

### Functions to add bootstraps to collections

def run_optimise_bootstrap_coords(boot_collection, ref_collection, ref_coords, task=_fast_geo, rooted=False, **kwargs):
    fit = np.empty((len(boot_collection), ref_coords.shape[1]))
    if ISPY3:
        query_trees = [PhyloTree(tree.encode(), rooted) for tree in boot_collection.trees]
        ref_trees = [PhyloTree(tree.encode(), rooted) for tree in ref_collection.trees]
    else:
        query_trees = [PhyloTree(tree, rooted) for tree in boot_collection.trees]
        ref_trees = [PhyloTree(tree, rooted) for tree in ref_collection.trees]
    for i, tree in enumerate(query_trees):
        ref_dists = np.array([task(tree, ref_tree, False) for ref_tree in ref_trees])
        opt = OptimiseDistanceFit(ref_coords.values, ref_dists)
        fit[i] = opt.newton(**kwargs)
    return fit

def run_out_of_sample_mds(boot_collection, ref_collection, ref_distance_matrix, index, dimensions, task=_fast_geo, rooted=False, **kwargs):
    """
    index = index of the locus the bootstrap sample corresponds to - only important if
            using recalc=True in kwargs
    """
    fit = np.empty((len(boot_collection), dimensions))

# Path: treeCl/tasks.py
# def _fast_geo(tree1, tree2, normalise=False):
#     return getGeodesicDistance(tree1, tree2, normalise)
# 
# Path: treeCl/constants.py
# ISPY3 = sys.version_info.major>=3


# Path: treeCl/bootstrap.py
import numpy as np
from scipy.spatial.distance import pdist, squareform
from .tasks import _fast_geo
from .constants import ISPY3
from tree_distance import PhyloTree

    x_new = x
    x_old = x-1 # dummy value
    while np.abs(x_new - x_old).sum() > tolerance:
        x_old = x_new
        step_size = golden_section_search(lambda step: f(grad_desc_update(x_old, a, c, step), a, c), -1.0, 1.0)
        x_new = grad_desc_update(x_old, a, c, step_size)
    return x_new

### Functions to add bootstraps to collections

def run_optimise_bootstrap_coords(boot_collection, ref_collection, ref_coords, task=_fast_geo, rooted=False, **kwargs):
    fit = np.empty((len(boot_collection), ref_coords.shape[1]))
    if ISPY3:
        query_trees = [PhyloTree(tree.encode(), rooted) for tree in boot_collection.trees]
        ref_trees = [PhyloTree(tree.encode(), rooted) for tree in ref_collection.trees]
    else:
        query_trees = [PhyloTree(tree, rooted) for tree in boot_collection.trees]
        ref_trees = [PhyloTree(tree, rooted) for tree in ref_collection.trees]
    for i, tree in enumerate(query_trees):
        ref_dists = np.array([task(tree, ref_tree, False) for ref_tree in ref_trees])
        opt = OptimiseDistanceFit(ref_coords.values, ref_dists)
        fit[i] = opt.newton(**kwargs)
    return fit

def run_out_of_sample_mds(boot_collection, ref_collection, ref_distance_matrix, index, dimensions, task=_fast_geo, rooted=False, **kwargs):
    """
    index = index of the locus the bootstrap sample corresponds to - only important if
            using recalc=True in kwargs
    """
    fit = np.empty((len(boot_collection), dimensions))

    if ISPY3: