"""Collection of useful data transforms."""

# Imports
import numpy as np
import Neuron

import scipy
import scipy.linalg   # SciPy Linear Algebra Library
from numpy.linalg import inv


def get_leaves(nodes, parents):
    """
    Compute the list of leaf nodes.
    Parameters
    ----------
    nodes: list
        list of all nodes in the tree
    parents: list
        list of parents for each node
    Returns
    -------
    leaves: list
        sorted list of leaf nodes
    """
    leaves = np.sort(list(set(nodes) - set(parents)))
    return leaves


def encode_prufer(parents, verbose=0):
    """
    Convert the parents sequence to a prufer sequence.
    Parameters
    ----------
    parents: list
        list of parents for each node
    verbose: bool
        default is False
    Returns
    -------
    prufer: list
        corresponding prufer sequence
    """
    n_nodes = len(parents)
    nodes = range(n_nodes)

    prufer = list()
    for n in range(n_nodes - 2):

        # Recalculate all the leaves
        leaves = get_leaves(nodes, parents)
        if verbose:
            print 'leaves', leaves

        # Add the parent of the lowest numbered leaf to the sequence
        leaf_idx = np.where(nodes == leaves[0])[0][0]
        prufer.append(parents[leaf_idx])
        if verbose:
            print 'prufer', prufer

        # Remove the lowest numbered leaf and its corresponding parent
        del nodes[leaf_idx]
        del parents[leaf_idx]