Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions iglu_python/adrr.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import warnings

import numpy as np
import pandas as pd

from .utils import check_data_columns


def adrr(data: pd.DataFrame|pd.Series) -> pd.DataFrame|float:
"""
Calculate average daily risk range (ADRR)
Expand All @@ -13,7 +13,7 @@ def adrr(data: pd.DataFrame|pd.Series) -> pd.DataFrame|float:

Parameters
----------
data : pd.DataFrame|pd.Series
data : pd.DataFrame|pd.Series
DataFrame object with column names "id", "time", and "gl".
or a Timeseries of glucose values.

Expand Down Expand Up @@ -83,7 +83,7 @@ def adrr_single(data: pd.DataFrame|pd.Series) -> float:
data_filtered = data.dropna()
if len(data_filtered) == 0:
return np.nan

# Group by date and calculate daily risk for each day
daily_risks = data_filtered.groupby(data_filtered.index.date).apply(
lambda x: _calculate_daily_risk(x)
Expand Down
4 changes: 2 additions & 2 deletions iglu_python/ea1c.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def ea1c_single(data: pd.Series) -> float:
"""Calculate eA1C for a single subject"""
if not isinstance(data, pd.Series):
raise ValueError("Data must be a pandas Series")

data = data.dropna()
if len(data) == 0:
return np.nan

return (46.7 + data.mean()) / 28.7
return (46.7 + data.mean()) / 28.7
9 changes: 5 additions & 4 deletions iglu_python/grade_eugly.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def grade_eugly(
data: Union[pd.DataFrame, pd.Series, np.ndarray, list], lower: int = 70, upper: int = 140
) -> pd.DataFrame|float:
) -> pd.DataFrame|float:
"""
Calculate percentage of GRADE score attributable to target range.

Expand All @@ -20,7 +20,8 @@ def grade_eugly(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
lower : int, default=70
Lower bound used for hypoglycemia cutoff, in mg/dL
upper : int, default=140
Expand Down Expand Up @@ -63,7 +64,7 @@ def grade_eugly(
if isinstance(data, (np.ndarray, list)):
data = pd.Series(data)
return grade_eugly_single(data, lower, upper)

# Handle DataFrame input
data = check_data_columns(data)

Expand Down Expand Up @@ -91,4 +92,4 @@ def grade_eugly_single(data: pd.Series, lower: int = 70, upper: int = 140) -> fl
return np.nan

eugly_percent = (np.sum(grade_scores[in_range]) / total_grade) * 100
return eugly_percent
return eugly_percent
7 changes: 4 additions & 3 deletions iglu_python/grade_hyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def grade_hyper(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], upper: i
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
upper : int, default=140
Upper bound used for hyperglycemia cutoff, in mg/dL

Expand Down Expand Up @@ -75,12 +76,12 @@ def grade_hyper_single(data: pd.Series, upper: int = 140) -> float:

# Calculate GRADE scores
grade_scores = _grade_formula(data)

# Calculate percentage above upper bound
above_upper = data > upper
total_grade = np.sum(grade_scores)
if total_grade == 0:
return np.nan

hyper_percent = (np.sum(grade_scores[above_upper]) / total_grade) * 100
return hyper_percent
return hyper_percent
9 changes: 5 additions & 4 deletions iglu_python/grade_hypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def grade_hypo(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], lower: in
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
lower : int, default=80
Lower bound used for hypoglycemia cutoff, in mg/dL

Expand Down Expand Up @@ -58,7 +59,7 @@ def grade_hypo(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], lower: in
if isinstance(data, (np.ndarray, list)):
data = pd.Series(data)
return grade_hypo_single(data, lower)

# Handle DataFrame input
data = check_data_columns(data)

Expand All @@ -77,12 +78,12 @@ def grade_hypo_single(data: pd.Series, lower: int = 80) -> float:

# Calculate GRADE scores
grade_scores = _grade_formula(data)

# Calculate percentage below lower bound
below_lower = data < lower
total_grade = np.sum(grade_scores)
if total_grade == 0:
return np.nan

hypo_percent = (np.sum(grade_scores[below_lower]) / total_grade) * 100
return hypo_percent
return hypo_percent
4 changes: 2 additions & 2 deletions iglu_python/gvp.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def gvp(data: Union[pd.DataFrame, pd.Series]) -> pd.DataFrame|float:
return gvp_single(data)

# Handle DataFrame input
data = check_data_columns(data)
data = check_data_columns(data)
data.set_index("time", inplace=True, drop=True)

out = data.groupby('id').agg(
Expand Down Expand Up @@ -138,4 +138,4 @@ def gvp_single(subj_data):
if base_length == 0:
return np.nan

return (np.sum(added_length) / base_length - 1) * 100
return (np.sum(added_length) / base_length - 1) * 100
4 changes: 2 additions & 2 deletions iglu_python/hbgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def hbgi(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.DataFrame
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values

Returns
Expand Down Expand Up @@ -88,4 +88,4 @@ def calculate_hbgi_single(glucose_values: pd.Series) -> float:
n = len(glucose_values)
hbgi_value = 10 * np.sum(fbg[glucose_values >= 112.5] ** 2) / n

return hbgi_value
return hbgi_value
5 changes: 3 additions & 2 deletions iglu_python/hyper_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def hyper_index(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
ULTR : int, default=140
Upper Limit of Target Range, in mg/dL
a : float, default=1.1
Expand Down Expand Up @@ -90,4 +91,4 @@ def hyper_index_single(
hyper_values = gl[gl > ULTR] - ULTR
hyper_index = np.sum(hyper_values**a) / (len(gl) * c)

return hyper_index
return hyper_index
4 changes: 2 additions & 2 deletions iglu_python/hypo_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def hypo_index(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
LLTR : int, default=80
Lower Limit of Target Range, in mg/dL
Expand Down Expand Up @@ -67,7 +67,7 @@ def hypo_index(
if isinstance(data, (np.ndarray, list)):
data = pd.Series(data)
return hypo_index_single(data, LLTR, b, d)

data = check_data_columns(data)
out = data.groupby('id').agg(
hypo_index = ("gl", lambda x: hypo_index_single(x, LLTR, b, d))
Expand Down
17 changes: 9 additions & 8 deletions iglu_python/igc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def igc(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
LLTR : int, default=80
Lower Limit of Target Range, in mg/dL
ULTR : int, default=140
Expand Down Expand Up @@ -86,12 +87,12 @@ def igc(
return out

def igc_single(
gl: pd.Series,
LLTR: int = 80,
ULTR: int = 140,
a: float = 1.1,
b: float = 2,
c: int = 30,
gl: pd.Series,
LLTR: int = 80,
ULTR: int = 140,
a: float = 1.1,
b: float = 2,
c: int = 30,
d: int = 30
) -> float:
"""
Expand All @@ -102,4 +103,4 @@ def igc_single(
out_hypo = hypo_index(gl, LLTR=LLTR, b=b, d=d)

out = out_hyper + out_hypo
return out
return out
7 changes: 4 additions & 3 deletions iglu_python/iqr_glu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def iqr_glu(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.DataFr
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values

Returns
-------
Expand Down Expand Up @@ -51,7 +52,7 @@ def iqr_glu(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.DataFr
return np.nan
# Calculate IQR for Series
iqr_val = iqr_glu_single(data)
return iqr_val
return iqr_val

# Handle DataFrame input
data = check_data_columns(data)
Expand Down Expand Up @@ -85,4 +86,4 @@ def iqr_glu_single(
return np.nan
# Calculate IQR for Series
iqr_val = np.percentile(gl, 75) - np.percentile(gl, 25)
return iqr_val
return iqr_val
4 changes: 2 additions & 2 deletions iglu_python/j_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def j_index(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.DataFr
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values

Returns
Expand Down Expand Up @@ -78,4 +78,4 @@ def j_index_single(gl: pd.Series) -> float:

# Calculate J-index
j_index = 0.001 * (mean_gl + sd_gl) ** 2
return j_index
return j_index
3 changes: 2 additions & 1 deletion iglu_python/lbgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def lbgi(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.DataFrame
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns ['id', 'time', 'gl'] or Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns ['id', 'time', 'gl'] or Series of glucose values,
or a numpy array or list of glucose values
in mg/dL

Returns
Expand Down
3 changes: 2 additions & 1 deletion iglu_python/m_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def m_value(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], r: float = 9
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
r : float, default=90
A reference value corresponding to basal glycemia in normal subjects

Expand Down
6 changes: 3 additions & 3 deletions iglu_python/mad_glu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def mad_glu(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
constant : float, default=1.4826
Scaling factor to multiply the MAD value. The default value of 1.4826
Expand All @@ -31,7 +31,7 @@ def mad_glu(
pd.DataFrame|float
DataFrame with columns:
- id: subject identifier (if DataFrame input)
- MAD: MAD value (median absolute deviation of glucose values).
- MAD: MAD value (median absolute deviation of glucose values).
If a Series of glucose values is passed, then a float is returned.

Examples
Expand Down Expand Up @@ -74,4 +74,4 @@ def mad_glu_single(gl: pd.Series, constant: float = 1.4826) -> float:
if len(gl) == 0:
return np.nan
mad_val = np.median(np.abs(gl - np.median(gl))) * constant
return mad_val
return mad_val
4 changes: 2 additions & 2 deletions iglu_python/median_glu.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Union

import pandas as pd
import numpy as np
import pandas as pd

from .utils import check_data_columns

Expand All @@ -17,7 +17,7 @@ def median_glu(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.Dat
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values

Returns
Expand Down
4 changes: 2 additions & 2 deletions iglu_python/modd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def modd(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
lag : int, default=1
Integer indicating which lag (# days) to use. Default is 1.
Expand All @@ -31,7 +31,7 @@ def modd(
pd.DataFrame|float
DataFrame with columns:
- id: subject identifier (if DataFrame input)
- MODD: Mean of Daily Differences value.
- MODD: Mean of Daily Differences value.
If a Series of glucose values is passed, then a float is returned.

References
Expand Down
2 changes: 1 addition & 1 deletion iglu_python/pgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def pgs(
Parameters
----------
data : Union[pd.DataFrame, pd.Series]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
Should only be data for 1 subject. In case multiple subject ids are detected,
a warning is produced and only 1st subject is used.
Expand Down
2 changes: 1 addition & 1 deletion iglu_python/quantile_glu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def quantile_glu(
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values
quantiles : List[float], default=[0, 25, 50, 75, 100]
List of quantile values between 0 and 100
Expand Down
2 changes: 1 addition & 1 deletion iglu_python/range_glu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def range_glu(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.Data
Parameters
----------
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
or a numpy array or list of glucose values

Returns
Expand Down
Loading
Loading