Skip to content

Commit

Permalink
Parameterize window length
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuicai committed Aug 23, 2023
1 parent f454e16 commit d798849
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
16 changes: 8 additions & 8 deletions simglucose/analysis/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ def percent_stats(BG, ax=None):
return p_stats, fig, ax


def risk_index_trace(df_BG, sample_rate, visualize=False):
chunk_BG = [df_BG.iloc[i:i + sample_rate, :] for i in range(0, len(df_BG), sample_rate)]
def risk_index_trace(df_BG, sample_time=3, window_length=60, visualize=False):
step_size = int(window_length / sample_time) # window size set to 1 hour for calculating Risk Index
chunk_BG = [df_BG.iloc[i:i + step_size, :] for i in range(0, len(df_BG), step_size)]

if len(chunk_BG[-1]) != sample_rate: # Remove the last chunk which is not full
if len(chunk_BG[-1]) != step_size: # Remove the last chunk which is not full
chunk_BG.pop()

fBG = [
Expand Down Expand Up @@ -250,13 +251,12 @@ def CVGA(BG_list, label=None):
def report(df, cgm_sensor=None, save_path=None):
BG = df.unstack(level=0).BG

sample_rate = 20 # Use 20 as default sample rate in one hour
if cgm_sensor is not None:
sample_rate = int(60 / cgm_sensor.sample_time)

fig_ensemble, ax1, ax2, ax3 = ensemblePlot(df)
pstats, fig_percent, ax4 = percent_stats(BG)
ri_per_hour, ri_mean, fig_ri, ax5 = risk_index_trace(BG, sample_rate, visualize=False)
if cgm_sensor is not None:
ri_per_hour, ri_mean, fig_ri, ax5 = risk_index_trace(BG, sample_time=cgm_sensor.sample_time, visualize=False)
else:
ri_per_hour, ri_mean, fig_ri, ax5 = risk_index_trace(BG, visualize=False)
zone_stats, fig_cvga, ax6 = CVGA(BG, label='')
axes = [ax1, ax2, ax3, ax4, ax5, ax6]
figs = [fig_ensemble, fig_percent, fig_ri, fig_cvga]
Expand Down
15 changes: 7 additions & 8 deletions tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@ def setUp(self):
def test_risk_index_trace(self):
BG = self.df.unstack(level=0).BG
sample_time = CGMSensor.withName("Dexcom").sample_time
sample_rate = int(60 / sample_time)
ri_per_hour, ri_mean, fig, axes = risk_index_trace(BG, sample_rate)
ri_per_hour, ri_mean, fig, axes = risk_index_trace(BG, sample_time=sample_time)

LBGI = ri_per_hour.transpose().LBGI
HBGI = ri_per_hour.transpose().HBGI
RI = ri_per_hour.transpose()["Risk Index"]

self.assertEqual(LBGI.size, 48)
self.assertEqual(LBGI.iloc[-1].test, 0.8429957158900777)
self.assertEqual(LBGI.iloc[0].test, 0.0)
self.assertEqual(round(LBGI.iloc[-1].test, 3), 0.843)
self.assertEqual(round(LBGI.iloc[0].test, 3), 0.0)

self.assertEqual(HBGI.size, 48)
self.assertEqual(HBGI.iloc[-1].test, 0.0)
self.assertEqual(HBGI.iloc[0].test, 2.755277346918188)
self.assertEqual(round(HBGI.iloc[-1].test,3), 0.0)
self.assertEqual(round(HBGI.iloc[0].test,3), 2.755)

self.assertEqual(RI.size, 48)
self.assertEqual(RI.iloc[-1].test, 0.8429957158900777)
self.assertEqual(RI.iloc[0].test, 2.755277346918188)
self.assertEqual(round(RI.iloc[-1].test,3), 0.843)
self.assertEqual(round(RI.iloc[0].test,3), 2.755)


if __name__ == '__main__':
Expand Down

0 comments on commit d798849

Please sign in to comment.