Skip to content

Commit 3aecc10

Browse files
committed
ruff cleanup
1 parent c7ba8c3 commit 3aecc10

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

iglu_python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .cv_measures import cv_measures
1010
from .ea1c import ea1c
1111
from .episode_calculation import episode_calculation
12+
from .extension.load_data import load_dexcom, load_libre
1213
from .gmi import gmi
1314
from .grade import grade
1415
from .grade_eugly import grade_eugly
@@ -41,7 +42,6 @@
4142
from .sd_roc import sd_roc
4243
from .summary_glu import summary_glu
4344
from .utils import CGMS2DayByDay, check_data_columns, gd2d_to_df, is_iglu_r_compatible, set_iglu_r_compatible
44-
from .extension.load_data import load_libre, load_dexcom
4545

4646
__all__ = [
4747
"above_percent",

iglu_python/extension/load_data.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from pathlib import Path
7+
78
import 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

tests/test_load_data.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
Tests the functionality of loading CGM data from device-specific files.
55
"""
66

7-
import pytest
8-
import pandas as pd
9-
import numpy as np
10-
from pathlib import Path
11-
import tempfile
127
import os
8+
import tempfile
9+
from pathlib import Path
10+
11+
import numpy as np
12+
import pandas as pd
13+
import pytest
1314

1415
# Import the module to test
15-
from iglu_python import load_libre, load_dexcom
16+
from iglu_python import load_dexcom, load_libre
17+
1618

1719
@pytest.fixture(scope="module")
1820
def test_data_paths():
@@ -136,7 +138,7 @@ def test_load_dexcom_glucose_statistics(test_data_paths):
136138
ts_01 = load_dexcom(str(test_data_paths['dexcom_eur_01']))
137139
ts_02 = load_dexcom(str(test_data_paths['dexcom_eur_02']))
138140
ts_03 = load_dexcom(str(test_data_paths['dexcom_eur_03']))
139-
141+
140142
for ts in [ts_01, ts_02, ts_03]:
141143
# Convert to numeric for statistics
142144
numeric_values = pd.to_numeric(ts, errors='coerce').dropna()
@@ -186,7 +188,7 @@ def test_load_libre_time_interval(test_data_paths):
186188
expected_interval = pd.Timedelta(minutes=15)
187189
tolerance = pd.Timedelta(minutes=5)
188190
close_intervals = time_diffs[abs(time_diffs - expected_interval) <= tolerance]
189-
assert len(close_intervals) / len(time_diffs) > 0.8 # At least 80% should be close
191+
assert len(close_intervals) / len(time_diffs) > 0.8 # At least 80% should be close
190192

191193
def test_load_dexcom_time_interval(test_data_paths):
192194
timeseries = load_dexcom(str(test_data_paths['dexcom_eur_01']))
@@ -211,4 +213,4 @@ def test_load_dexcom_numeric_values(test_data_paths):
211213
# Check that all values are numeric
212214
assert pd.api.types.is_numeric_dtype(timeseries)
213215
# Check that there are no NaN values (all should be valid numbers)
214-
assert not timeseries.isna().any()
216+
assert not timeseries.isna().any()

0 commit comments

Comments
 (0)