Skip to content

Instantly share code, notes, and snippets.

View ThonyPrice's full-sized avatar

Thony Price ThonyPrice

View GitHub Profile
@ThonyPrice
ThonyPrice / violin-plt-2.py
Created May 15, 2019 20:58
Medium - Get started analysing your CGM data
z = df.copy(deep=True)
z = z.join(pd.get_dummies(z['Status']), how='left')
day_map = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
z['weekday'] = z.index.weekday
z = z[['weekday', 'glucose']]
z = z.resample('D').mean()
z['chunk'] = 'Second'
z['chunk'].iloc[:(z.shape[0]//2)] = 'First'
z.sort_values(['weekday', 'chunk'], inplace=True)
@ThonyPrice
ThonyPrice / heatmap-plt.py
Created April 25, 2019 20:27
Medium - Get started analysing your CGM data
z = df.copy(deep=True)
z['day'] = z.index.date
z['day_time'] = z.index.round('5Min').time
z = z[['day', 'day_time', 'glucose']]
z = z.groupby(['day', 'day_time']).last()
z = z.reset_index()
_ = plt.figure(figsize=(20, 8))
x_cmap = sns.diverging_palette(220, 20, n=9)
f = sns.heatmap(z.pivot('day_time', 'day', 'glucose'), cmap=x_cmap, vmin=0, vmax=300)
@ThonyPrice
ThonyPrice / violin-plt-1.py
Created April 25, 2019 20:06
Medium - Get started analysing your CGM data
z = df.copy(deep=True)
z = z.join(pd.get_dummies(z['Status']), how='left')
day_map = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
z['weekday'] = z.index.weekday
z = z[['weekday', 'glucose']]
z = z.resample('D').mean()
z.sort_values('weekday', inplace=True)
z.dropna(inplace=True)
z['weekday'] = z['weekday'].apply(lambda x: day_map[int(x)])
@ThonyPrice
ThonyPrice / avg-day-plt.py
Created April 25, 2019 19:51
Medium - Get started analysing your CGM data
z = df.copy(deep=True)
z.index = z.index.round('5Min')
plt.figure(figsize=(16, 6))
ax = sns.lineplot(x=z.index.time, y=z['glucose'], color='#13ac5f')
@ThonyPrice
ThonyPrice / apply-range.py
Last active February 23, 2020 05:14
Medium - Get started analysing your CGM data
def applyThreshold(val, urgentLow=54, low=70, high=180):
if val > high:
return 'high'
elif val <= high and val >= low:
return 'in_range'
elif val < low and val > urgentLow:
return 'low'
else:
return 'urgent_low';
@ThonyPrice
ThonyPrice / glucose-trend.py
Created April 25, 2019 18:22
Medium - Get started analysing your CGM data
z = df.copy(deep=True)
z = z.resample('D').mean()
z['date'] = z.index.date
z.insert(z.shape[1], 'day', z.index.value_counts().sort_index().cumsum())
plt.figure(figsize=(16, 6))
fs = sns.regplot(
data = z,
x ='day',
y ='glucose',
@ThonyPrice
ThonyPrice / glucose-dist.py
Created April 25, 2019 17:49
Medium - Get started analysing your CGM data
df['glucose'] = df['glucose'].astype(int)
_ = sns.distplot(df['glucose'])
df.describe()
@ThonyPrice
ThonyPrice / missing-data.py
Created April 25, 2019 17:07
Medium - Get started analysing your CGM data
expected_diff = timedelta(minutes=5, seconds=30)
missing_samples_idxs = np.where(z['t_diff'] > expected_diff)[0]
c_samples_times = z.iloc[missing_samples_idxs, :]['t_diff']
c_samples_times
@ThonyPrice
ThonyPrice / close-samples.py
Created April 25, 2019 16:43
Medium - Get started analysing your CGM data
# Inspect samples closer than average
import numpy as np
import matplotlib.pyplot as plt
expected_diff = timedelta(minutes=4, seconds=30)
close_samples_idxs = np.where(z['t_diff'] < expected_diff)[0]
c_samples_times = z.iloc[close_samples_idxs, :]['t_diff']
c_samples_times = c_samples_times.apply(lambda x: x.seconds)
@ThonyPrice
ThonyPrice / expected_freq.py
Created April 24, 2019 23:48
Medium - Get started analysing your CGM data
from datetime import datetime, date
z = df.copy(deep=True)
z['t1'] = z.index
z['t2'] = z['t1'].shift(1)
z['t_diff'] = z['t1'] - z['t2']
z['t_diff'].describe()