import os from matplotlib import pyplot as plt from glucose_func import glucose_readings, insulin_readings # --- Extracting data and creating data dict --- hw_dir = (r'C:\Users\angel\OneDrive - George Mason University\BENG 320\BENG 320 Project\Diabetes-Data') os.chdir(hw_dir) #changes directory data_dict = {} for i in range(1,10): with open('data-0'+str(i)+'.txt', 'r') as data: datalines = data.readlines() data_dict['Data-0' + str(i)] = datalines for i in range(10,71): with open('data-'+str(i)+'.txt', 'r') as data: datalines = data.readlines() data_dict['Data-' + str(i)] = datalines # --- Creating time and values lists, and code, date, and patient_data dict ---- patient_data = {} for i, k in enumerate(data_dict): patient_data[k] = {} #code values for list_index in range(len(data_dict[k])): data_dict[k][list_index] = data_dict[k][list_index].split('\t') patient_data[k][data_dict[k][list_index][2]] = {} #value is date patient_data[k].pop('4', None) patient_data[k].pop('56', None) patient_data[k].pop('65', None) patient_data[k].pop('69', None) patient_data[k].pop('66', None) for ind, key in enumerate(patient_data[k]): for list_ind in range(len(data_dict[k])): if key == data_dict[k][list_ind][2]: patient_data[k][key][data_dict[k][list_ind][0]] = [[], []] patient_data[k][key][data_dict[k][list_ind][0]][0].append(data_dict[k][list_ind][1]) patient_data[k][key][data_dict[k][list_ind][0]][1].append(data_dict[k][list_ind][3]) code_meanings = {'33': 'Regular insulin dose', '34': 'NPH insulin dose', '35': 'UltraLente insulin dose', '43': 'Unspecified blood glucose measurement, mg/dl', '57': 'Unspecified blood glucose measurement, mg/dl', '58': 'Pre-breakfast blood glucose measurement, mg/dl', '59': 'Post-breakfast blood glucose measurement, mg/dl', '60': 'Pre-lunch blood glucose measurement, mg/dl', '61': 'Post-lunch blood glucose measurement, mg/dl', '62': 'Pre-supper blood glucose measurement, mg/dl', '63': 'Post-supper blood glucose measurement, mg/dl', '64': 'Pre-snack blood glucose measurement, mg/dl', '65': 'Hypoglycemic symptoms', '66': 'Typical meal ingestion', '67': 'More-than-usual meal ingestion', '68': 'Less-than-usual meal ingestion', '69': 'Typical exercise activity', '70': 'More-than-usual exercise activity', '71': 'Less-than-usual exercise activity', '72': 'Unspecified special event', '48':'Unspecified blood glucose measurement' } #plotting figures for each patient, time being the x axis and the value with their BGM being the y axis. # --- This is for patient 52 --- for num_index in range(52,53): #num_index = 52 name = 'Data-'+str(num_index) #Data-52 fig, ax = plt.subplots(2,2) # fig.tight_layout() fig.text(0.5, 0.04, 'Time, HH:MM', ha='center', va='center') fig.text(0.06, 0.5, 'Blood Glucose Measurements, mg/dl', ha='center', va='center', rotation='vertical') fig.subplots_adjust(left=0.150, right=0.9, bottom=0.125, top=0.9, wspace=0.3, hspace=0.5) arrow_properties = dict( facecolor="black", width=0.3, headwidth=2, shrink=0.01) times, val = glucose_readings(12, 20, data_dict, name) ax[0,0].plot(times, val, label= '04-22-1991') ax[0,0].annotate('More-than-usual exercise', ('11:30', 50), arrowprops=arrow_properties) ax[0,0].legend() times, val = glucose_readings(20, 29, data_dict, name) ax[0,1].plot(times, val, label='04-23-1991') ax[0,1].annotate('More-than-usual exercise', ('05:26', 55), arrowprops=arrow_properties) ax[0,1].legend() times, val = glucose_readings(29, 36, data_dict, name) ax[1,0].plot(times, val, label='04-24-1991') ax[1,0].annotate('More-than-usual exercise', ('05:35', 45), arrowprops=arrow_properties) ax[1,0].legend() times, val = glucose_readings(46, 53, data_dict, name) ax[1,1].plot(times, val, label='04-26-1991') ax[1,1].annotate('More-than-usual exercise', ('16:20', 90), arrowprops=arrow_properties) ax[1,1].legend() fig.suptitle('Blood glucose measurements on different days, before and after rigorous exercise (' + name +')') #--- PATIENT 57 --- for num_index in range(57,58): #num_index = 57 name = 'Data-'+str(num_index) #Data-52 fig, ax = plt.subplots(2) # fig.tight_layout() fig.text(0.5, 0.04, 'Time, HH:MM', ha='center', va='center') fig.text(0.06, 0.5, 'Blood Glucose Measurements, mg/dl', ha='center', va='center', rotation='vertical') fig.subplots_adjust(left=0.150, right=0.9, bottom=0.125, top=0.9, wspace=0.3, hspace=0.5) arrow_properties = dict( facecolor="black", width=0.3, headwidth=2, shrink=0.01) times, val = glucose_readings(93, 102, data_dict, name) ax[0].plot(times, val, label= '08-09-1990') ax[0].annotate('Less-than-usual exercise', ('19:23', 230), arrowprops=arrow_properties) ax[0].legend() times, val = glucose_readings(125, 133, data_dict, name) ax[1].plot(times, val, label='08-12-1990') ax[1].annotate('Less-than-usual exercise', ('16:56', 210), arrowprops=arrow_properties) ax[1].legend() fig.suptitle('Blood glucose measurements on different days, before and after less-than-usual exercise (' + name +')') #--- PATIENT 59 --- for num_index in range(59,60): #num_index = 59 name = 'Data-'+str(num_index) #Data-52 fig, ax = plt.subplots() # fig.tight_layout() fig.text(0.5, 0.04, 'Time, HH:MM', ha='center', va='center') fig.text(0.06, 0.5, 'Blood Glucose Measurements, mg/dl', ha='center', va='center', rotation='vertical') fig.subplots_adjust(left=0.150, right=0.9, bottom=0.125, top=0.9, wspace=0.3, hspace=0.5) arrow_properties = dict( facecolor="black", width=0.3, headwidth=2, shrink=0.01) times, val = glucose_readings(12, 23, data_dict, name) ax.plot(times, val, label= '09-05-1990') ax.annotate('Less-than-usual exercise', ('16:33', 180), arrowprops=arrow_properties) ax.legend() fig.suptitle('Blood glucose measurements on different days, before and after less-than-usual exercise (' + name +')') #--- PATIENT 61 --- for num_index in range(57,58): #num_index = 61 name = 'Data-'+str(num_index) #Data-61 fig, ax = plt.subplots() # fig.tight_layout() fig.text(0.5, 0.04, 'Time, HH:MM', ha='center', va='center') fig.text(0.06, 0.5, 'Blood Glucose Measurements, mg/dl', ha='center', va='center', rotation='vertical') fig.subplots_adjust(left=0.150, right=0.9, bottom=0.125, top=0.9, wspace=0.3, hspace=0.5) arrow_properties = dict( facecolor="black", width=0.3, headwidth=2, shrink=0.01) times, val = glucose_readings(92, 101, data_dict, name) ax.plot(times, val, label='05-28-1991') ax.annotate('More-than-usual exercise', ('19:23', 230), arrowprops=arrow_properties) ax.legend() fig.suptitle('Blood glucose measurements on different days, before and after more-than-usual exercise (' + name +')') #--- Showing the differences between insulin taken based off of exercise #--- Patient 52 --- for num_index in range(52,53): #num_index = 52 name = 'Data-'+str(num_index) #Data-52 fig, ax = plt.subplots(2) # fig.tight_layout() fig.text(0.5, 0.04, 'Time, HH:MM', ha='center', va='center') fig.text(0.06, 0.5, 'Glucose Measurements, mg/dl', ha='center', va='center', rotation='vertical') fig.subplots_adjust(left=0.150, right=0.9, bottom=0.125, top=0.9, wspace=0.3, hspace=0.5) arrow_properties = dict( facecolor="black", width=0.3, headwidth=1, shrink=0.01) times, val = glucose_readings(12, 20, data_dict, name) #this is for insulin readings ax[0].plot(times, val, label='04-22-1991') ax[0].annotate('Regular insulin dose', ('05:44', 165), arrowprops=arrow_properties) ax[0].annotate('NPH insulin dose', ('05:44', 150), arrowprops=arrow_properties) ax[0].annotate('More-than-usual exercise', ('11:30', 50), arrowprops=arrow_properties) ax[0].legend() times, val = glucose_readings(20, 29, data_dict, name) #this is for insulin readings ax[1].plot(times, val, label='04-23-1991') ax[1].annotate('Ultralente insulin dose', ('05:26', 55), arrowprops=arrow_properties) ax[1].annotate('NPH insulin dose', ('05:26', 65), arrowprops=arrow_properties) ax[1].annotate('More-than-usual exercise', ('05:26', 45), arrowprops=arrow_properties) ax[1].legend() fig.suptitle('Blood glucose measurements and the effects of different insulin doses (' + name +')')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question