Source code for tools

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
This module contains useful tools to facilitate some repeated calculations.
"""

from __future__ import (division, print_function, absolute_import,
                        unicode_literals)
import numpy as np


__all__ = ["nearest_index", ]


[docs]def nearest_index(array, target_value): """ Finds the index of a value in ``array`` that is closest to ``target_value``. Parameters ---------- array (``numpy.array``): Target array. target_value (``float``): Target value. Returns ------- index (``int``): Index of the value in ``array`` that is closest to ``target_value``. """ index = array.searchsorted(target_value) index = np.clip(index, 1, len(array) - 1) left = array[index - 1] right = array[index] index -= target_value - left < right - target_value return index