-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgludelaymap.py
413 lines (375 loc) · 18.7 KB
/
gludelaymap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# A Poincare plot is a type of non-linear scatter-plot of regular time series data plotted as a function of phase
# such that for any given point:
# the x-coordinate is the measured value at index n and y-coordinate is the measured value at index n + 1,
# [(n,n+1),(n+1,n+2), (n+2,n+3),...]
# Currently runs as a script that generates poincare plot from dexcom clarity csv file or libre csv
# a color density gradient avoids overplotting in a scatterplot https://www.data-to-viz.com/graph/density2d.html
# surface plot? https://yuchen52.medium.com/beyond-data-scientist-3d-plots-in-python-with-examples-2a8bd7aa654b
# https://kivymd.readthedocs.io/en/latest/components/charts/
import polars as pl
from re import match
from datetime import datetime
import tkinter as tk
from tkinter import filedialog
import matplotlib.pyplot as plt
from matplotlib import cm
from numpy import vstack
from scipy.stats import gaussian_kde
from ntpath import basename
import sys
from os import path
# import os
# https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', path.dirname(path.abspath(__file__)))
return path.join(base_path, relative_path)
# https://datagy.io/python-round-to-multiple/
def round_to_multiple(number, multiple):
return multiple * round(number / multiple)
def file_sel():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(title="CGM data selector",
filetypes=(("csv files", "*.csv"), ("all files", "*.*")))
return file_path
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# print(resource_path('pbio.2005143.s010'))
delays = input(
'Enter delay values you would like to plot (values must be a multiple of 5 and comma seperated, e.g. 5,15,25):')
delays_list = delays.split(',')
a = len(delays_list)
control = input('Should a reference plot of non-diabetic control data be shown? (y/n): ')
if control == ('y' or 'Y' or 'yes' or 'Yes' or 'YES'):
control = True
b = 2
else:
b = 1
control = False
color_legend = input('Display color density function legend for each plot? (y/n): ')
if color_legend == ('y' or 'Y' or 'yes' or 'Yes' or 'YES'):
color_legend = True
else:
color_legend = False
daterange = input('Would you like to select a range of dates to analyze? (n = use all data): ')
if daterange == ('y' or 'Y' or 'yes' or 'Yes' or 'YES'):
daterange = True
else:
daterange = False
axes_scale = input('Auto-detect axes scale? (y/n): ')
if axes_scale == ('n' or 'no' or 'NO' or 'No'):
x_origin = int(input('Enter x-axis origin (e.g. 0) : '))
xdomain = int(input('Enter x-axis domain (e.g. 300) : '))
y_origin = int(input('Enter y-axis origin (e.g. 0) : '))
yrange = int(input('Enter y-axis range (e.g. 300) : '))
axes_scale = True
else:
axes_scale = False
# total number of plots
numplots = a * b
pattern = 'Index.*'
patternl = '.*[AP]M.*'
numplotslist = list(range(numplots))
x = numplotslist.copy()
y = numplotslist.copy()
z = numplotslist.copy()
xy = numplotslist.copy()
idx = numplotslist.copy()
# a = number of delay points, b= 1 for data alone, 2 w control
fig, ax = plt.subplots(a, b, sharex=True, sharey=True)
fig.suptitle('Color Density Poincaré Plot')
if control:
# every other plot is control
controlplots = numplotslist[0::2]
patientplots = numplotslist[1::2]
# control data public cgm data from hall et al.
# https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.2005143
df = pl.read_csv(resource_path('pbio.2005143.s010'), sep='\t', has_header=True, parse_dates=True)
# control data
date0 = df.select('DisplayTime').row(0)[0]
df = df.with_columns(
(pl.col('DisplayTime') - date0).alias('duration')
)
df = df.with_columns(
pl.col('duration').dt.minutes().alias('dminutes')
)
df = df.with_columns(
pl.col('dminutes').apply(lambda q: round_to_multiple(q, 5)).alias('deltamins')
)
df = df.filter(pl.col('subjectId') == '1636-69-035')
df = df.select([pl.col('deltamins'), pl.col('GlucoseValue')])
df = df.unique(subset='deltamins', keep='first')
# control data plots
for index, plot in enumerate(controlplots):
if len(controlplots) == 1:
subp = ax[0]
else:
subp = ax[index, 0]
delay = int(delays_list[index])
dd = df.select('deltamins') + delay
djoiny = df.join(dd, on='deltamins', how='inner')
djoiny = djoiny.with_columns(
pl.col('GlucoseValue').alias('GlucoseValue2')
)
# now in order to get the x coordinate, we need to subtract 15 from the y coordinate
de = djoiny.select('deltamins') - delay
djoinx = df.join(de, on='deltamins', how='inner')
djoinx = djoinx.select(['GlucoseValue'])
djoiny = djoiny.select(['GlucoseValue2'])
djoinxy = pl.concat(
[
djoinx,
djoiny,
],
how='horizontal'
)
# color options https://matplotlib.org/2.0.2/examples/color/colormaps_reference.html
x[plot] = djoinxy.get_column('GlucoseValue').to_numpy()
y[plot] = djoinxy.get_column('GlucoseValue2').to_numpy()
xy[plot] = vstack([x[plot], y[plot]])
z[plot] = gaussian_kde(xy[plot])(xy[plot])
idx[plot] = z[plot].argsort()
x[plot], y[plot], z[plot] = x[plot][idx[plot]], y[plot][idx[plot]], z[plot][idx[plot]]
if index == 0:
subp.set_title("normal control")
subp.set_xlabel("n")
subp.set_ylabel("n + " + str(delays_list[index]) + " mins")
subp.grid(color='green', linestyle='--', linewidth=0.25)
con_plot = subp.scatter(x[plot], y[plot], c=z[plot], s=50, cmap=cm.jet, alpha=.6, marker='.')
subp.axline((0, 0), slope=1, color='black', linestyle=':', linewidth=.5, alpha=.7)
plt.colorbar(con_plot)
# this works for libre but need to manually input usa or euro, maybe query user for country??
# need to update for dexcom
# load pt data
csvimport = file_sel()
di = pl.read_csv(csvimport, has_header=False)
cgmtype = di.select('column_1').row(0)[0]
if match(pattern, cgmtype):
if cgmtype == 'Index':
# this is a clarity file so....
df = pl.read_csv(csvimport, has_header=True, try_parse_dates=True,
null_values=['Low', 'High', 'NotComputable', 'SensorNotActive', 'SensorWarmup',
'OutOfRange', 'NoSensor', 'InvalidReading', 'SensorFailed',
'SensorInitializing', 'SensorCalibration'])
else:
df = pl.read_csv(csvimport, has_header=True, try_parse_dates=True, sep='\t',
null_values=['Low', 'High', 'NotComputable', 'SensorNotActive', 'SensorWarmup',
'OutOfRange', 'NoSensor', 'InvalidReading', 'SensorFailed',
'SensorInitializing', 'SensorCalibration'])
df = df.lazy().drop_nulls('Transmitter ID').collect()
df = df.with_columns(
(pl.col('Glucose Value (mg/dL)').cast(pl.Int64)).alias('glu'))
df = df.lazy().drop_nulls('glu').collect()
df = df.sort('Timestamp (YYYY-MM-DDThh:mm:ss)')
date0 = df.select('Timestamp (YYYY-MM-DDThh:mm:ss)').row(0)[0]
df = df.with_columns(
(pl.col('Timestamp (YYYY-MM-DDThh:mm:ss)') - date0).dt.minutes().alias('duration')
)
df = df.with_columns(
(pl.col('Timestamp (YYYY-MM-DDThh:mm:ss)')).alias('timestamp')
)
else:
# this is a libre file so....
# we need a regex to detect whether or not 'PM or AM' is in the date column
df = pl.read_csv(csvimport, has_header=True, try_parse_dates=True, skip_rows=1)
datetest = df.select('Device Timestamp').row(0)[0]
if match(patternl, datetest):
df = df.with_columns(
(pl.col('Device Timestamp').str.strptime(pl.Datetime, fmt='%m-%d-%Y %I:%M %p')).alias('timestamp'))
# print('usa version')
else:
# print('euro version')
df = df.with_columns(
(pl.col('Device Timestamp').str.strptime(pl.Datetime, fmt='%d-%m-%Y %H:%M')).alias('timestamp'))
df = df.with_columns(
(pl.col('Scan Glucose mg/dL').cast(pl.Int64).fill_null(0) + pl.col('Historic Glucose mg/dL')).alias(
'glu'))
df = df.lazy().drop_nulls('glu').collect()
df = df.sort('timestamp')
date0 = df.select('timestamp').row(0)[0]
df = df.with_columns(
(pl.col('timestamp') - date0).dt.minutes().alias('duration')
)
df = df.with_columns(
pl.col('duration').apply(lambda q: round_to_multiple(q, 5)).alias('deltamins')
)
if daterange:
dstart = df.head(1).select('timestamp')
dend = df.tail(1).select('timestamp')
print('Date Range of uploaded file is from ' + str(dstart[0, 0]) + ' to ' + str(dend[0, 0]))
analysis_start = input('Enter analysis start date/time (following format YYYY-MM-DD HH:mm): ')
analysis_end = input('Enter analysis end date/time (same format): ')
start_datetime = datetime.strptime(analysis_start, '%Y-%m-%d %H:%M')
end_datetime = datetime.strptime(analysis_end, '%Y-%m-%d %H:%M')
df = df.filter(
pl.col("timestamp").is_between(start_datetime, end_datetime)
)
df = df.select(['deltamins', 'glu'])
# remove duplicates
df = df.unique(subset='deltamins', keep='first')
# patient data plots
for index, plot in enumerate(patientplots):
if len(patientplots) == 1:
subpt = ax[1]
else:
subpt = ax[index, 1]
delay = int(delays_list[index])
# djoiny gives y coordinate in glu col, returned table is - deltamins, glu
dd = df.select('deltamins') + delay
djoiny = df.join(dd, on='deltamins', how='inner')
djoiny = djoiny.with_columns(
pl.col('glu').alias('glu2')
)
# now in order to get the x coordinate, we need to subtract 15 from the y coordinate
de = djoiny.select('deltamins') - delay
djoinx = df.join(de, on='deltamins', how='inner')
djoinx = djoinx.select(['glu'])
djoiny = djoiny.select(['glu2'])
djoinxy = pl.concat(
[
djoinx,
djoiny,
],
how='horizontal'
)
if len(djoinxy) < 5:
print("not enough data to plot")
exit()
# color options https://matplotlib.org/2.0.2/examples/color/colormaps_reference.html
x[plot] = djoinxy.get_column('glu').to_numpy()
y[plot] = djoinxy.get_column('glu2').to_numpy()
xy[plot] = vstack([x[plot], y[plot]])
z[plot] = gaussian_kde(xy[plot])(xy[plot])
# Sort the points by density, so that the densest points are plotted last
idx[plot] = z[plot].argsort()
x[plot], y[plot], z[plot] = x[plot][idx[plot]], y[plot][idx[plot]], z[plot][idx[plot]]
if index == 0:
subpt.set_title("file: " + basename(csvimport), size=8)
subpt.set_xlabel("n")
if axes_scale:
subpt.set_xlim(x_origin, xdomain)
subpt.set_ylim(y_origin, yrange)
# subpt.set_xlim(0,200)
# subpt.set_ylim(0,200)
subpt.grid(color='green', linestyle='--', linewidth=0.25)
exp_plot = subpt.scatter(x[plot], y[plot], c=z[plot], s=50, cmap=cm.jet, alpha=.6, marker='.')
subpt.axline((0, 0), slope=1, color='black', linestyle=':', linewidth=.5, alpha=.7)
plt.colorbar(exp_plot, label='probability density function')
else:
# patient data plots
patientplots = numplotslist
# load pt data
csvimport = file_sel()
di = pl.read_csv(csvimport, has_header=False)
cgmtype = di.select('column_1').row(0)[0]
if match(pattern, cgmtype):
# this is a clarity file so....
if cgmtype == 'Index':
df = pl.read_csv(csvimport, has_header=True, try_parse_dates=True,
null_values=['Low', 'High', 'NotComputable', 'SensorNotActive', 'SensorWarmup',
'OutOfRange', 'NoSensor', 'InvalidReading', 'SensorFailed',
'SensorInitializing', 'SensorCalibration'])
else:
df = pl.read_csv(csvimport, has_header=True, try_parse_dates=True, sep='\t',
null_values=['Low', 'High', 'NotComputable', 'SensorNotActive', 'SensorWarmup',
'OutOfRange', 'NoSensor', 'InvalidReading', 'SensorFailed',
'SensorInitializing', 'SensorCalibration'])
df = df.lazy().drop_nulls('Transmitter ID').collect()
df = df.with_columns(
(pl.col('Glucose Value (mg/dL)').cast(pl.Int64)).alias('glu'))
df = df.lazy().drop_nulls('glu').collect()
df = df.sort('Timestamp (YYYY-MM-DDThh:mm:ss)')
date0 = df.select('Timestamp (YYYY-MM-DDThh:mm:ss)').row(0)[0]
df = df.with_columns(
(pl.col('Timestamp (YYYY-MM-DDThh:mm:ss)') - date0).dt.minutes().alias('duration')
)
df = df.with_columns(
(pl.col('Timestamp (YYYY-MM-DDThh:mm:ss)')).alias('timestamp')
)
else:
# this is a libre file so....
df = pl.read_csv(csvimport, has_header=True, try_parse_dates=True, skip_rows=1)
datetest = df.select('Device Timestamp').row(0)[0]
if match(patternl, datetest):
df = df.with_columns(
(pl.col('Device Timestamp').str.strptime(pl.Datetime, fmt='%m-%d-%Y %I:%M %p')).alias('timestamp'))
# print('usa version')
else:
# print('euro version')
df = df.with_columns(
(pl.col('Device Timestamp').str.strptime(pl.Datetime, fmt='%d-%m-%Y %H:%M')).alias('timestamp'))
df = df.with_columns(
(pl.col('Scan Glucose mg/dL').cast(pl.Int64).fill_null(0) + pl.col('Historic Glucose mg/dL')).alias(
'glu'))
df = df.lazy().drop_nulls('glu').collect()
df = df.sort('timestamp')
date0 = df.select('timestamp').row(0)[0]
df = df.with_columns(
(pl.col('timestamp') - date0).dt.minutes().alias('duration')
)
df = df.with_columns(
pl.col('duration').apply(lambda q: round_to_multiple(q, 5)).alias('deltamins')
)
if daterange:
dstart = df.head(1).select('timestamp')
dend = df.tail(1).select('timestamp')
print('Date Range of uploaded file is from ' + str(dstart[0, 0]) + ' to ' + str(dend[0, 0]))
analysis_start = input('Enter analysis start date/time (following format YYYY-MM-DD HH:mm): ')
analysis_end = input('Enter analysis end date/time (same format): ')
start_datetime = datetime.strptime(analysis_start, '%Y-%m-%d %H:%M')
end_datetime = datetime.strptime(analysis_end, '%Y-%m-%d %H:%M')
df = df.filter(
pl.col("timestamp").is_between(start_datetime, end_datetime)
)
df = df.select(['deltamins', 'glu'])
# remove duplicates
df = df.unique(subset='deltamins', keep='first')
# patient data plots
for index, plot in enumerate(patientplots):
if len(patientplots) == 1:
subpt = ax
else:
subpt = ax[index]
delay = int(delays_list[index])
# djoiny gives y coordinate in glu col, returned table is - deltamins, glu
dd = df.select('deltamins') + delay
djoiny = df.join(dd, on='deltamins', how='inner')
djoiny = djoiny.with_columns(
pl.col('glu').alias('glu2')
)
# now in order to get the x coordinate, we need to subtract 15 from the y coordinate
de = djoiny.select('deltamins') - delay
djoinx = df.join(de, on='deltamins', how='inner')
djoinx = djoinx.select(['glu'])
djoiny = djoiny.select(['glu2'])
djoinxy = pl.concat(
[
djoinx,
djoiny,
],
how='horizontal'
)
if len(djoinxy) < 5:
print("not enough data to plot")
exit()
# color options https://matplotlib.org/2.0.2/examples/color/colormaps_reference.html
x[plot] = djoinxy.get_column('glu').to_numpy()
y[plot] = djoinxy.get_column('glu2').to_numpy()
xy[plot] = vstack([x[plot], y[plot]])
z[plot] = gaussian_kde(xy[plot])(xy[plot])
# Sort the points by density, so that the densest points are plotted last
idx[plot] = z[plot].argsort()
x[plot], y[plot], z[plot] = x[plot][idx[plot]], y[plot][idx[plot]], z[plot][idx[plot]]
if index == 0:
subpt.set_title("file: " + basename(csvimport), size=8)
subpt.set_xlabel("n")
subpt.set_ylabel("n + " + str(delays_list[index]) + " mins")
if axes_scale:
subpt.set_xlim(x_origin, xdomain)
subpt.set_ylim(y_origin, yrange)
subpt.grid(color='green', linestyle='--', linewidth=0.25)
exp_plot = subpt.scatter(x[plot], y[plot], c=z[plot], s=50, cmap=cm.jet, alpha=.6, marker='.')
subpt.axline((0, 0), slope=1, color='black', linestyle=':', linewidth=.5, alpha=.7)
plt.colorbar(exp_plot, label='probability density function')
plt.show()