pychemstation.utils.num_utils

 1from typing import Tuple
 2
 3import numpy as np
 4
 5
 6def find_nearest_value_index(array, value) -> Tuple[float, int]:
 7    """Returns closest value and its index in a given array.
 8
 9    :param array: An array to search in.
10    :type array: np.array(float)
11    :param value: Target value.
12    :type value: float
13
14    :return: Nearest value in array and its index.
15    """
16
17    index_ = np.argmin(np.abs(array - value))
18    return array[index_], index_
19
20
21def interpolate_to_index(array, ids, precision: int = 100) -> np.ndarray:
22    """Find value in between arrays elements.
23
24    Constructs linspace of size "precision" between index+1 and index to
25    find approximate value for array[index], where index is float number.
26    Used for 2D data, where default scipy analysis occurs along one axis only,
27    e.g. signal.peak_width.
28
29    Rough equivalent of array[index], where index is float number.
30
31    :param array: Target array.
32    :type array: np.array(float)
33    :param ids: An array with "intermediate" indexes to interpolate to.
34    :type ids: np.array[float]
35    :param precision: Desired presion.
36
37    :return: New array with interpolated values according to provided indexes "ids".
38
39    Example:
40        >>> interpolate_to_index(np.array([1.5]), np.array([1, 2, 3], 100))
41            ... array([2.50505051])
42    """
43
44    # breaking ids into fractional and integral parts
45    prec, ids = np.modf(ids)
46
47    # rounding and switching type to int
48    prec = np.around(prec * precision).astype("int32")
49    ids = ids.astype("int32")
50
51    # linear interpolation for each data point
52    # as (n x m) matrix where n is precision and m is number of indexes
53    space = np.linspace(array[ids], array[ids + 1], precision)
54
55    # due to rounding error the index may become 100 in (100, ) array
56    # as a consequence raising IndexError when such array is indexed
57    # therefore index 100 will become the last (-1)
58    prec[prec == 100] = -1
59
60    # precise slicing
61    true_values = np.array(
62        [space[:, index[0]][value] for index, value in np.ndenumerate(prec)]
63    )
64
65    return true_values
def find_nearest_value_index(array, value) -> Tuple[float, int]:
 7def find_nearest_value_index(array, value) -> Tuple[float, int]:
 8    """Returns closest value and its index in a given array.
 9
10    :param array: An array to search in.
11    :type array: np.array(float)
12    :param value: Target value.
13    :type value: float
14
15    :return: Nearest value in array and its index.
16    """
17
18    index_ = np.argmin(np.abs(array - value))
19    return array[index_], index_

Returns closest value and its index in a given array.

Parameters
  • array: An array to search in.
  • value: Target value.
Returns

Nearest value in array and its index.

def interpolate_to_index(array, ids, precision: int = 100) -> numpy.ndarray:
22def interpolate_to_index(array, ids, precision: int = 100) -> np.ndarray:
23    """Find value in between arrays elements.
24
25    Constructs linspace of size "precision" between index+1 and index to
26    find approximate value for array[index], where index is float number.
27    Used for 2D data, where default scipy analysis occurs along one axis only,
28    e.g. signal.peak_width.
29
30    Rough equivalent of array[index], where index is float number.
31
32    :param array: Target array.
33    :type array: np.array(float)
34    :param ids: An array with "intermediate" indexes to interpolate to.
35    :type ids: np.array[float]
36    :param precision: Desired presion.
37
38    :return: New array with interpolated values according to provided indexes "ids".
39
40    Example:
41        >>> interpolate_to_index(np.array([1.5]), np.array([1, 2, 3], 100))
42            ... array([2.50505051])
43    """
44
45    # breaking ids into fractional and integral parts
46    prec, ids = np.modf(ids)
47
48    # rounding and switching type to int
49    prec = np.around(prec * precision).astype("int32")
50    ids = ids.astype("int32")
51
52    # linear interpolation for each data point
53    # as (n x m) matrix where n is precision and m is number of indexes
54    space = np.linspace(array[ids], array[ids + 1], precision)
55
56    # due to rounding error the index may become 100 in (100, ) array
57    # as a consequence raising IndexError when such array is indexed
58    # therefore index 100 will become the last (-1)
59    prec[prec == 100] = -1
60
61    # precise slicing
62    true_values = np.array(
63        [space[:, index[0]][value] for index, value in np.ndenumerate(prec)]
64    )
65
66    return true_values

Find value in between arrays elements.

Constructs linspace of size "precision" between index+1 and index to find approximate value for array[index], where index is float number. Used for 2D data, where default scipy analysis occurs along one axis only, e.g. signal.peak_width.

Rough equivalent of array[index], where index is float number.

Parameters
  • array: Target array.
  • ids: An array with "intermediate" indexes to interpolate to.
  • precision: Desired presion.
Returns

New array with interpolated values according to provided indexes "ids".

Example:

interpolate_to_index(np.array([1.5]), np.array([1, 2, 3], 100)) ... array([2.50505051])