pychemstation.analysis.utils

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