Skip to content

Instantly share code, notes, and snippets.

View ThonyPrice's full-sized avatar

Thony Price ThonyPrice

View GitHub Profile
@ThonyPrice
ThonyPrice / access-drive.py
Last active April 24, 2019 19:22
Medium - Get started analysing your CGM data
from google.colab import drive
drive.mount('/content/drive')
@ThonyPrice
ThonyPrice / read-data.py
Last active May 30, 2019 19:31
Medium - Get started analysing your CGM data
import pandas as pd
df = pd.read_csv('drive/My Drive/my-cgm-exploration/Example_CGM_data.csv')
df.head(20)
@ThonyPrice
ThonyPrice / trim-df.py
Created April 24, 2019 21:10
Medium - Get started analysing your CGM data
df = df.iloc[10:,:]
df.head()
@ThonyPrice
ThonyPrice / select-df.py
Created April 24, 2019 21:31
Medium - Get started analysing your CGM data
df = df[['Timestamp (YYYY-MM-DDThh:mm:ss)', 'Glucose Value (mg/dL)']]
df.rename(columns={
"Timestamp (YYYY-MM-DDThh:mm:ss)": "time",
"Glucose Value (mg/dL)": "glucose"},
inplace=True)
df.head()
@ThonyPrice
ThonyPrice / date-time-index.py
Created April 24, 2019 22:38
Medium - Get started analysing your CGM data
df = df.copy(deep=True)
df['time'] = pd.to_datetime(df['time'])
df['time'] = df['time']\
.dt.tz_localize(tz='UTC')\
.dt.tz_convert('US/Pacific')
df.set_index('time', inplace=True)
df.head()
@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()
@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 / 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 / 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 / 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',