|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import matplotlib.pyplot as plt |
| 6 | +import numpy as np |
6 | 7 | import pandas as pd |
7 | 8 |
|
8 | 9 |
|
@@ -38,7 +39,7 @@ def plot_daily(cgm_timeseries: pd.Series, lower: int = 70, upper: int = 140) -> |
38 | 39 | axes[i].plot(cgm_one_day.index, cgm_one_day.values) |
39 | 40 | axes[i].set_title(f"Day: {day.strftime('%Y-%m-%d')}") |
40 | 41 | axes[i].set_ylabel("Glucose (mg/dL)") |
41 | | - axes[i].set_ylim(0, max(max(cgm_one_day.values), 300)) |
| 42 | + axes[i].set_ylim(0, max(np.nanmax(cgm_one_day.values), 300)) |
42 | 43 |
|
43 | 44 | # Fill area above upper limit and plot it in orange |
44 | 45 | upper_array = [upper] * len(cgm_one_day.values) |
@@ -66,3 +67,82 @@ def plot_daily(cgm_timeseries: pd.Series, lower: int = 70, upper: int = 140) -> |
66 | 67 |
|
67 | 68 | fig.tight_layout() |
68 | 69 | return fig |
| 70 | + |
| 71 | +def plot_statistics(cgm_timeseries: pd.Series, lower: int = 70, upper: int = 140) -> plt.Figure: |
| 72 | + """ |
| 73 | + Plot statistical representation of daily trends |
| 74 | + in the single 24h timeline, this will plot mean sample trends, 10%, +25% and 75% and 90% quantiles |
| 75 | + """ |
| 76 | + # check if cgm_timeseries is a pandas series |
| 77 | + if not isinstance(cgm_timeseries, pd.Series): |
| 78 | + raise AttributeError("cgm_timeseries must be a pandas series") |
| 79 | + |
| 80 | + # check if cgm_timeseries is not a datetime index |
| 81 | + if not isinstance(cgm_timeseries.index, pd.DatetimeIndex): |
| 82 | + raise AttributeError("cgm_timeseries must have a datetime index") |
| 83 | + |
| 84 | + # check if cgm_timeseries is not empty |
| 85 | + if len(cgm_timeseries) < 16: |
| 86 | + raise ValueError("cgm_timeseries is too short to plot statistics") |
| 87 | + |
| 88 | + # get sampling frequency |
| 89 | + time_diffs = cgm_timeseries.index.to_series().diff() |
| 90 | + dt0 = int(time_diffs.mode().iloc[0].total_seconds() / 60) |
| 91 | + |
| 92 | + |
| 93 | + # Create time grid |
| 94 | + start_time = cgm_timeseries.index.min().floor("D") |
| 95 | + end_time = cgm_timeseries.index.max().ceil("D") |
| 96 | + time_grid = pd.date_range(start=start_time, end=end_time, freq=f"{dt0}min") |
| 97 | + # remove the last time point |
| 98 | + time_grid = time_grid[:-1] |
| 99 | + |
| 100 | + # interpolate |
| 101 | + cgm_timeseries_interpolated = np.interp( |
| 102 | + (time_grid - start_time).total_seconds() / 60, |
| 103 | + (cgm_timeseries.index - start_time).total_seconds() / 60, |
| 104 | + cgm_timeseries.values, |
| 105 | + left=np.nan, |
| 106 | + right=np.nan, |
| 107 | + ) |
| 108 | + |
| 109 | + # reorganise as 2d array with rows as timepoints and columns as days |
| 110 | + # Reshape to days |
| 111 | + n_days = (end_time - start_time).days |
| 112 | + n_points_per_day = 24 * 60 // dt0 |
| 113 | + cgm_timeseries_2d = cgm_timeseries_interpolated.reshape(n_days, n_points_per_day) |
| 114 | + |
| 115 | + # one day time grid |
| 116 | + time_grid_one_day = time_grid[0:n_points_per_day] |
| 117 | + # get mean sample trends |
| 118 | + mean_sample_trends = np.nanmean(cgm_timeseries_2d, axis=0) |
| 119 | + |
| 120 | + # get 10%, +25% and 75% and 90% quantiles |
| 121 | + quantiles = np.nanpercentile(cgm_timeseries_2d, [10, 25, 75, 90], axis=0) |
| 122 | + |
| 123 | + # create figure and axes |
| 124 | + fig, ax = plt.subplots(figsize=(12, 6)) |
| 125 | + |
| 126 | + # plot mean sample trends |
| 127 | + ax.plot(time_grid_one_day,mean_sample_trends, color="orange", alpha=1, linewidth=3, label='Mean sample trends') |
| 128 | + |
| 129 | + # plot quantiles |
| 130 | + ax.fill_between(time_grid_one_day, quantiles[0], quantiles[1], alpha=0.25, color="blue",label='10% quantile') |
| 131 | + ax.fill_between(time_grid_one_day, quantiles[1], mean_sample_trends, alpha=0.50, color="blue",label='25% quantile') |
| 132 | + ax.fill_between(time_grid_one_day, mean_sample_trends, quantiles[2], alpha=0.50, color="blue",label='75% quantile') |
| 133 | + ax.fill_between(time_grid_one_day, quantiles[2], quantiles[3], alpha=0.25, color="blue",label='90% quantile') |
| 134 | + |
| 135 | + ax.axhline(y=upper, color="orange", linestyle="--", alpha=0.7, label=f"Hyper threshold ({upper} mg/dL)") |
| 136 | + ax.axhline(y=lower, color="green", linestyle="--", alpha=0.7, label=f"Hypo threshold ({lower} mg/dL)") |
| 137 | + |
| 138 | + ax.set_ylim(min(30, np.nanmin(cgm_timeseries.values)), max(np.nanmax(cgm_timeseries.values), 300)) |
| 139 | + ax.set_xlabel("Time (hours)") |
| 140 | + time_grid_one_day = pd.date_range(start=start_time, periods=24, freq="1h") |
| 141 | + ax.set_xticks(time_grid_one_day) # Show every hour from 0 to 24 |
| 142 | + ax.set_xticklabels([f"{h.hour}" for h in time_grid_one_day]) # Format as HH:00 |
| 143 | + ax.grid(True, alpha=0.3, linestyle="--") |
| 144 | + ax.legend() |
| 145 | + fig.tight_layout() |
| 146 | + |
| 147 | + # plot the results |
| 148 | + return fig |
0 commit comments