44"""
55
66from pathlib import Path
7+
78import pandas as pd
89
910
@@ -43,7 +44,7 @@ def load_libre(file_path: str) -> pd.Series:
4344 format = '%m-%d-%Y %H:%M'
4445 elif 'Historic Glucose mmol/L' in df .columns :
4546 df = df .loc [:, ('Device Timestamp' , 'Historic Glucose mmol/L' , 'Scan Glucose mmol/L' )]
46- format = '%d-%m-%Y %I:%M %p'
47+ format = '%d-%m-%Y %I:%M %p'
4748 convert = True
4849 else :
4950 df = df = df .loc [:, ('Device Timestamp' , 'Historic Glucose mg/dL' , 'Scan Glucose mg/dL' )]
@@ -56,10 +57,10 @@ def load_libre(file_path: str) -> pd.Series:
5657
5758 # Convert glucose values to numeric
5859 df ['glc' ] = pd .to_numeric (df ['glc' ], errors = 'coerce' )
59-
60+
6061 # convert to mg/dL if needed
6162 if convert :
62- df ['glc' ] = df ['glc' ] * 18.01559
63+ df ['glc' ] = df ['glc' ] * 18.01559
6364
6465 # Drop NaN values and sort by 'time'
6566 df = df .dropna (subset = ['time' , 'glc' ]).sort_values ('time' ).reset_index (drop = True )
@@ -97,46 +98,46 @@ def load_dexcom(file_path: str) -> pd.Series:
9798 # Drop top rows
9899 df = df .iloc [1 :]
99100 df .reset_index (inplace = True , drop = True )
100-
101+
101102 # Find timestamp column
102103 timestamp_cols = [col for col in df .columns if 'Timestamp' in str (col )]
103104 if not timestamp_cols :
104105 raise ValueError ("No timestamp column found in Dexcom data" )
105106 timestamp_col = timestamp_cols [0 ]
106-
107+
107108 # Find glucose column
108109 glucose_cols = [col for col in df .columns if 'Glucose' in str (col )]
109110 if not glucose_cols :
110111 raise ValueError ("No glucose column found in Dexcom data" )
111112 glucose_col = glucose_cols [0 ]
112-
113+
113114 # Check if conversion is needed (mmol/L to mg/dL)
114115 convert = False
115116 if 'mmol/L' in str (glucose_col ):
116117 convert = True
117-
118+
118119 # Select relevant columns
119120 df = df .loc [:, [timestamp_col , glucose_col ]]
120-
121+
121122 # Rename columns
122123 df .columns = ['time' , 'glc' ]
123-
124+
124125 # Convert 'time' column to datetime
125126 df ['time' ] = pd .to_datetime (df ['time' ], errors = 'coerce' )
126-
127+
127128 # Convert glucose values to numeric
128129 df ['glc' ] = pd .to_numeric (df ['glc' ], errors = 'coerce' )
129-
130+
130131 # Convert to mg/dL if needed
131132 if convert :
132133 df ['glc' ] = df ['glc' ] * 18.01559
133-
134+
134135 # Drop NaN values and sort by 'time'
135136 df = df .dropna (subset = ['time' , 'glc' ]).sort_values ('time' ).reset_index (drop = True )
136-
137+
137138 # Convert into timeseries
138139 timeseries = df .set_index ('time' )['glc' ]
139-
140+
140141 return timeseries
141142
142143
@@ -159,23 +160,23 @@ def _open_file(filepath: str) -> pd.DataFrame:
159160 if not Path (filepath ).exists ():
160161 raise FileNotFoundError (f"File not found: { filepath } " )
161162
162-
163+
163164 # Get file extension using basename
164165 extension = Path (filepath ).suffix
165-
166+
166167 try :
167168 if extension == '.csv' :
168169 # Assume that the user uploaded a CSV file
169- df = pd .read_csv (filepath , header = None , names = [ i for i in range (0 , 20 )] )
170+ df = pd .read_csv (filepath , header = None , names = list ( range (0 , 20 )) )
170171 elif extension == '.xls' or extension == '.xlsx' :
171172 # Assume that the user uploaded an Excel file
172- df = pd .read_excel (filepath , header = None , names = [ i for i in range (0 , 20 )] )
173+ df = pd .read_excel (filepath , header = None , names = list ( range (0 , 20 )) )
173174 elif extension == '.txt' or extension == '.tsv' :
174175 # Assume that the user uploaded a text file
175- df = pd .read_table (filepath , header = None , names = [ i for i in range (0 , 20 )] )
176+ df = pd .read_table (filepath , header = None , names = list ( range (0 , 20 )) )
176177 else :
177178 raise ValueError (f"Unsupported file extension: { extension } " )
178-
179+
179180 return df
180181 except Exception as e :
181- raise ValueError (f"Error reading file: { filepath } " ) from e
182+ raise ValueError (f"Error reading file: { filepath } " ) from e
0 commit comments