def _weight_by_vector(trajectories, w_vector):
    r"""weights the values of `trajectories` given a weighting vector
    `w_vector`.

    Each value in `trajectories` will be weighted by the 'rate of change'
    to 'optimal rate of change' ratio. The 'rate of change' of a vector
    measures how each point in the vector changes with respect to its
    predecessor point. The 'optimal rate of change' is the rate of change
    in which each point in the vector performs the same change than its
    predecessor, meaning that when calling this function over evenly spaced
    `w_vector` values, no change will be reflected on the output.

    Parameters
    ----------
    trajectories: pandas.DataFrame
        Values to weight
    w_vector: pandas.Series
        Values used to weight `trajectories`

    Returns
    -------
    pandas.DataFrame
        A weighted version of `trajectories`.

    Raises
    ------
    ValueError
        If `trajectories` and `w_vector` don't have equal lengths
        If `w_vector` is not a gradient
    TypeError
        If `trajectories` and `w_vector` are not iterables
    """
    try:
        if len(trajectories) != len(w_vector):
            raise ValueError("trajectories (%d) & w_vector (%d) must be equal "
                             "lengths" % (len(trajectories), len(w_vector)))
    except __HOLE__:
        raise TypeError("trajectories and w_vector must be iterables")

    # check no repeated values are passed in the weighting vector
    if len(set(w_vector)) != len(w_vector):
        raise ValueError("The weighting vector must be a gradient")

    # no need to weight in case of a one element vector
    if len(w_vector) == 1:
        return trajectories

    # Cast to float so divisions have a floating point resolution
    total_length = float(max(w_vector) - min(w_vector))

    # Reflects the expected gradient between subsequent values in w_vector
    # the first value isn't weighted so subtract one from the number of
    # elements
    optimal_gradient = total_length/(len(w_vector)-1)

    # for all elements apply the weighting function
    for i, idx in enumerate(trajectories.index):
        # Skipping the first element is it doesn't need to be weighted
        if i != 0:
            trajectories.ix[idx] = (trajectories.ix[idx] * optimal_gradient /
                                    (np.abs((w_vector[i] - w_vector[i-1]))))

    return trajectories
dataset/ETHPy150Open biocore/scikit-bio/skbio/stats/gradient.py/_weight_by_vector