def local_minima(fits, window=15):
    """
    Find the local minima within fits, and return them and their indices.

    Returns a list of indices at which the minima were found, and a list of the
    minima, sorted in order of increasing minimum.  The keyword argument window
    determines how close two local minima are allowed to be to one another.  If
    two local minima are found closer together than that, then the lowest of
    them is taken as the real minimum.  window=1 will return all local minima.

    """
    from scipy.ndimage.filters import minimum_filter as min_filter

    minfits = min_filter(fits, size=window, mode="wrap")

    minima = []
    for i in range(len(fits)):
        if fits[i] == minfits[i]:
            minima.append(fits[i])

    minima.sort()

    good_indices = [ fits.index(fit) for fit in minima ]
    good_fits = [ fit for fit in minima ]

    return(good_indices, good_fits)
def local_minima_fixed(fits, window=15):
    from scipy.ndimage.filters import minimum_filter as min_filter
    minfits = min_filter(fits, size=window, mode="wrap")
    minima_and_indices = []
    for i, (fit, minfit) in enumerate(zip(fits, minfits)):
        if fit == minfit:
            minima_and_indices.append([fit, i])
    minima_and_indices.sort()
    good_fits, good_indices = zip(*minima_and_indices)
    return good_indices, good_fits