# 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
    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:
        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):
        distvec = np.array([task(tree, ref_tree, False) for ref_tree in ref_trees])
        oos = OutOfSampleMDS(ref_distance_matrix)
        fit[i] = oos.fit(index, distvec, dimensions=dimensions, **kwargs)
    return fit


# 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

    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:
        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):
        distvec = np.array([task(tree, ref_tree, False) for ref_tree in ref_trees])
        oos = OutOfSampleMDS(ref_distance_matrix)
        fit[i] = oos.fit(index, distvec, dimensions=dimensions, **kwargs)
    return fit


def run_analytical_fit(boot_collection, ref_collection, ref_coords, task=_fast_geo, rooted=False, **kwargs):