kneeliverse.dfdt

The following module provides knee detection method based on DFDT algorithm.

  1# coding: utf-8
  2
  3'''
  4The following module provides knee detection method
  5based on DFDT algorithm.
  6'''
  7
  8__author__ = 'Mário Antunes'
  9__version__ = '1.0'
 10__email__ = 'mario.antunes@ua.pt'
 11__status__ = 'Development'
 12__license__ = 'MIT'
 13__copyright__ = '''
 14Copyright (c) 2021-2023 Stony Brook University
 15Copyright (c) 2021-2023 The Research Foundation of SUNY
 16'''
 17
 18import math
 19import logging
 20import numpy as np
 21import uts.gradient as grad
 22import uts.thresholding as thresh
 23import kneeliverse.multi_knee as mk
 24
 25
 26logger = logging.getLogger(__name__)
 27
 28
 29def get_knee(x: np.ndarray, y: np.ndarray) -> int:
 30    """
 31    Return the index of the knee point based on the DFDT method.
 32
 33    Args:
 34        x (np.ndarray): the value of the points in the x axis coordinates
 35        y (np.ndarray): the value of the points in the y axis coordinates
 36
 37    Returns:
 38        int: the index of the knee point
 39    """
 40    gradient = grad.cfd(x, y)
 41    return get_knee_gradient(gradient)
 42
 43
 44def get_knee_gradient(gradient: np.ndarray) -> int:
 45    """
 46    Return the index of the knee point based on the DFDT method.
 47
 48    Args:
 49        gradient (np.ndarray): the first order gradient of the trace points
 50
 51    Returns:
 52        int: the index of the knee point
 53    """
 54    t = thresh.isodata(gradient)
 55    diff = np.absolute(gradient - t)
 56    knee = np.argmin(diff[1:-1]) + 1
 57    return knee
 58
 59
 60def knee(points: np.ndarray) -> int:
 61    """
 62    Returns the index of the knee point based on the DFDT method.
 63
 64    It uses the iterative refinement  method.
 65
 66    Args:
 67        points (np.ndarray): numpy array with the points (x, y)
 68
 69    Returns:
 70        int: the index of the knee point
 71    """
 72    x = points[:, 0]
 73    y = points[:, 1]
 74
 75    gradient = grad.cfd(x, y)
 76
 77    knee = cutoff = 0
 78    last_knee = -1
 79
 80    while last_knee < knee and (len(x)-cutoff) > 2:
 81        last_knee = knee
 82        knee = get_knee_gradient(gradient[cutoff:]) + cutoff
 83        cutoff = int(math.ceil(knee/2.0))
 84
 85    return knee
 86
 87
 88def multi_knee(points: np.ndarray, t1: float = 0.001, t2: int = 3) -> np.ndarray:
 89    """
 90    Recursive knee point detection based on DFDT.
 91
 92    It returns the knee points on the curve.
 93
 94    Args:
 95        points (np.ndarray): numpy array with the points (x, y)
 96        t1 (float): coefficient of determination threshold (default 0.001)
 97        t2 (int): number of points threshold (default 3)
 98
 99    Returns:
100        np.ndarray: The knee points on the curve
101    """
102    return mk.multi_knee(knee, points, t1, t2)
logger = <Logger kneeliverse.dfdt (WARNING)>
def get_knee(x: numpy.ndarray, y: numpy.ndarray) -> int:
30def get_knee(x: np.ndarray, y: np.ndarray) -> int:
31    """
32    Return the index of the knee point based on the DFDT method.
33
34    Args:
35        x (np.ndarray): the value of the points in the x axis coordinates
36        y (np.ndarray): the value of the points in the y axis coordinates
37
38    Returns:
39        int: the index of the knee point
40    """
41    gradient = grad.cfd(x, y)
42    return get_knee_gradient(gradient)

Return the index of the knee point based on the DFDT method.

Arguments:
  • x (np.ndarray): the value of the points in the x axis coordinates
  • y (np.ndarray): the value of the points in the y axis coordinates
Returns:

int: the index of the knee point

def get_knee_gradient(gradient: numpy.ndarray) -> int:
45def get_knee_gradient(gradient: np.ndarray) -> int:
46    """
47    Return the index of the knee point based on the DFDT method.
48
49    Args:
50        gradient (np.ndarray): the first order gradient of the trace points
51
52    Returns:
53        int: the index of the knee point
54    """
55    t = thresh.isodata(gradient)
56    diff = np.absolute(gradient - t)
57    knee = np.argmin(diff[1:-1]) + 1
58    return knee

Return the index of the knee point based on the DFDT method.

Arguments:
  • gradient (np.ndarray): the first order gradient of the trace points
Returns:

int: the index of the knee point

def knee(points: numpy.ndarray) -> int:
61def knee(points: np.ndarray) -> int:
62    """
63    Returns the index of the knee point based on the DFDT method.
64
65    It uses the iterative refinement  method.
66
67    Args:
68        points (np.ndarray): numpy array with the points (x, y)
69
70    Returns:
71        int: the index of the knee point
72    """
73    x = points[:, 0]
74    y = points[:, 1]
75
76    gradient = grad.cfd(x, y)
77
78    knee = cutoff = 0
79    last_knee = -1
80
81    while last_knee < knee and (len(x)-cutoff) > 2:
82        last_knee = knee
83        knee = get_knee_gradient(gradient[cutoff:]) + cutoff
84        cutoff = int(math.ceil(knee/2.0))
85
86    return knee

Returns the index of the knee point based on the DFDT method.

It uses the iterative refinement method.

Arguments:
  • points (np.ndarray): numpy array with the points (x, y)
Returns:

int: the index of the knee point

def multi_knee(points: numpy.ndarray, t1: float = 0.001, t2: int = 3) -> numpy.ndarray:
 89def multi_knee(points: np.ndarray, t1: float = 0.001, t2: int = 3) -> np.ndarray:
 90    """
 91    Recursive knee point detection based on DFDT.
 92
 93    It returns the knee points on the curve.
 94
 95    Args:
 96        points (np.ndarray): numpy array with the points (x, y)
 97        t1 (float): coefficient of determination threshold (default 0.001)
 98        t2 (int): number of points threshold (default 3)
 99
100    Returns:
101        np.ndarray: The knee points on the curve
102    """
103    return mk.multi_knee(knee, points, t1, t2)

Recursive knee point detection based on DFDT.

It returns the knee points on the curve.

Arguments:
  • points (np.ndarray): numpy array with the points (x, y)
  • t1 (float): coefficient of determination threshold (default 0.001)
  • t2 (int): number of points threshold (default 3)
Returns:

np.ndarray: The knee points on the curve