Skip to content

Commit ba25bb2

Browse files
authored
Merge pull request #5 from staskh/all_metrics
All metrics completed, support for series, list and arrays as input
2 parents e883854 + 58535a8 commit ba25bb2

42 files changed

Lines changed: 292 additions & 278 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

iglu_python/adrr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import warnings
21

32
import numpy as np
43
import pandas as pd
54

65
from .utils import check_data_columns
76

7+
88
def adrr(data: pd.DataFrame|pd.Series) -> pd.DataFrame|float:
99
"""
1010
Calculate average daily risk range (ADRR)
@@ -13,7 +13,7 @@ def adrr(data: pd.DataFrame|pd.Series) -> pd.DataFrame|float:
1313
1414
Parameters
1515
----------
16-
data : pd.DataFrame|pd.Series
16+
data : pd.DataFrame|pd.Series
1717
DataFrame object with column names "id", "time", and "gl".
1818
or a Timeseries of glucose values.
1919
@@ -83,7 +83,7 @@ def adrr_single(data: pd.DataFrame|pd.Series) -> float:
8383
data_filtered = data.dropna()
8484
if len(data_filtered) == 0:
8585
return np.nan
86-
86+
8787
# Group by date and calculate daily risk for each day
8888
daily_risks = data_filtered.groupby(data_filtered.index.date).apply(
8989
lambda x: _calculate_daily_risk(x)

iglu_python/ea1c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ def ea1c_single(data: pd.Series) -> float:
7272
"""Calculate eA1C for a single subject"""
7373
if not isinstance(data, pd.Series):
7474
raise ValueError("Data must be a pandas Series")
75-
75+
7676
data = data.dropna()
7777
if len(data) == 0:
7878
return np.nan
7979

80-
return (46.7 + data.mean()) / 28.7
80+
return (46.7 + data.mean()) / 28.7

iglu_python/grade_eugly.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def grade_eugly(
1111
data: Union[pd.DataFrame, pd.Series, np.ndarray, list], lower: int = 70, upper: int = 140
12-
) -> pd.DataFrame|float:
12+
) -> pd.DataFrame|float:
1313
"""
1414
Calculate percentage of GRADE score attributable to target range.
1515
@@ -20,7 +20,8 @@ def grade_eugly(
2020
Parameters
2121
----------
2222
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
23-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
23+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
24+
or a numpy array or list of glucose values
2425
lower : int, default=70
2526
Lower bound used for hypoglycemia cutoff, in mg/dL
2627
upper : int, default=140
@@ -63,7 +64,7 @@ def grade_eugly(
6364
if isinstance(data, (np.ndarray, list)):
6465
data = pd.Series(data)
6566
return grade_eugly_single(data, lower, upper)
66-
67+
6768
# Handle DataFrame input
6869
data = check_data_columns(data)
6970

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

9394
eugly_percent = (np.sum(grade_scores[in_range]) / total_grade) * 100
94-
return eugly_percent
95+
return eugly_percent

iglu_python/grade_hyper.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def grade_hyper(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], upper: i
1717
Parameters
1818
----------
1919
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
20-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
20+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
21+
or a numpy array or list of glucose values
2122
upper : int, default=140
2223
Upper bound used for hyperglycemia cutoff, in mg/dL
2324
@@ -75,12 +76,12 @@ def grade_hyper_single(data: pd.Series, upper: int = 140) -> float:
7576

7677
# Calculate GRADE scores
7778
grade_scores = _grade_formula(data)
78-
79+
7980
# Calculate percentage above upper bound
8081
above_upper = data > upper
8182
total_grade = np.sum(grade_scores)
8283
if total_grade == 0:
8384
return np.nan
8485

8586
hyper_percent = (np.sum(grade_scores[above_upper]) / total_grade) * 100
86-
return hyper_percent
87+
return hyper_percent

iglu_python/grade_hypo.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def grade_hypo(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], lower: in
1717
Parameters
1818
----------
1919
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
20-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
20+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
21+
or a numpy array or list of glucose values
2122
lower : int, default=80
2223
Lower bound used for hypoglycemia cutoff, in mg/dL
2324
@@ -58,7 +59,7 @@ def grade_hypo(data: Union[pd.DataFrame, pd.Series, np.ndarray, list], lower: in
5859
if isinstance(data, (np.ndarray, list)):
5960
data = pd.Series(data)
6061
return grade_hypo_single(data, lower)
61-
62+
6263
# Handle DataFrame input
6364
data = check_data_columns(data)
6465

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

7879
# Calculate GRADE scores
7980
grade_scores = _grade_formula(data)
80-
81+
8182
# Calculate percentage below lower bound
8283
below_lower = data < lower
8384
total_grade = np.sum(grade_scores)
8485
if total_grade == 0:
8586
return np.nan
8687

8788
hypo_percent = (np.sum(grade_scores[below_lower]) / total_grade) * 100
88-
return hypo_percent
89+
return hypo_percent

iglu_python/gvp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def gvp(data: Union[pd.DataFrame, pd.Series]) -> pd.DataFrame|float:
6363
return gvp_single(data)
6464

6565
# Handle DataFrame input
66-
data = check_data_columns(data)
66+
data = check_data_columns(data)
6767
data.set_index("time", inplace=True, drop=True)
6868

6969
out = data.groupby('id').agg(
@@ -138,4 +138,4 @@ def gvp_single(subj_data):
138138
if base_length == 0:
139139
return np.nan
140140

141-
return (np.sum(added_length) / base_length - 1) * 100
141+
return (np.sum(added_length) / base_length - 1) * 100

iglu_python/hbgi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def hbgi(data: Union[pd.DataFrame, pd.Series, np.ndarray, list]) -> pd.DataFrame
2525
Parameters
2626
----------
2727
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
28-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
28+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
2929
or a numpy array or list of glucose values
3030
3131
Returns
@@ -88,4 +88,4 @@ def calculate_hbgi_single(glucose_values: pd.Series) -> float:
8888
n = len(glucose_values)
8989
hbgi_value = 10 * np.sum(fbg[glucose_values >= 112.5] ** 2) / n
9090

91-
return hbgi_value
91+
return hbgi_value

iglu_python/hyper_index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def hyper_index(
2020
Parameters
2121
----------
2222
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
23-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
23+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
24+
or a numpy array or list of glucose values
2425
ULTR : int, default=140
2526
Upper Limit of Target Range, in mg/dL
2627
a : float, default=1.1
@@ -90,4 +91,4 @@ def hyper_index_single(
9091
hyper_values = gl[gl > ULTR] - ULTR
9192
hyper_index = np.sum(hyper_values**a) / (len(gl) * c)
9293

93-
return hyper_index
94+
return hyper_index

iglu_python/hypo_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def hypo_index(
2020
Parameters
2121
----------
2222
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
23-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
23+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
2424
or a numpy array or list of glucose values
2525
LLTR : int, default=80
2626
Lower Limit of Target Range, in mg/dL
@@ -67,7 +67,7 @@ def hypo_index(
6767
if isinstance(data, (np.ndarray, list)):
6868
data = pd.Series(data)
6969
return hypo_index_single(data, LLTR, b, d)
70-
70+
7171
data = check_data_columns(data)
7272
out = data.groupby('id').agg(
7373
hypo_index = ("gl", lambda x: hypo_index_single(x, LLTR, b, d))

iglu_python/igc.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def igc(
2626
Parameters
2727
----------
2828
data : Union[pd.DataFrame, pd.Series, np.ndarray, list]
29-
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values, or a numpy array or list of glucose values
29+
DataFrame with columns 'id', 'time', and 'gl', or a Series of glucose values,
30+
or a numpy array or list of glucose values
3031
LLTR : int, default=80
3132
Lower Limit of Target Range, in mg/dL
3233
ULTR : int, default=140
@@ -86,12 +87,12 @@ def igc(
8687
return out
8788

8889
def igc_single(
89-
gl: pd.Series,
90-
LLTR: int = 80,
91-
ULTR: int = 140,
92-
a: float = 1.1,
93-
b: float = 2,
94-
c: int = 30,
90+
gl: pd.Series,
91+
LLTR: int = 80,
92+
ULTR: int = 140,
93+
a: float = 1.1,
94+
b: float = 2,
95+
c: int = 30,
9596
d: int = 30
9697
) -> float:
9798
"""
@@ -102,4 +103,4 @@ def igc_single(
102103
out_hypo = hypo_index(gl, LLTR=LLTR, b=b, d=d)
103104

104105
out = out_hyper + out_hypo
105-
return out
106+
return out

0 commit comments

Comments
 (0)