Skip to content

Commit ab55a57

Browse files
committed
active_percentage fixes
1 parent c03fe7a commit ab55a57

2 files changed

Lines changed: 69 additions & 27 deletions

File tree

iglu_python/active_percent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pandas as pd
55

6-
from .utils import check_data_columns
6+
from .utils import check_data_columns, localize_naive_timestamp
77

88

99
def active_percent(
@@ -121,7 +121,7 @@ def active_percent(
121121

122122
# Handle consistent end date if provided
123123
if consistent_end_date is not None:
124-
end_date = pd.to_datetime(consistent_end_date)
124+
end_date = localize_naive_timestamp(pd.to_datetime(consistent_end_date))
125125
start_date = end_date - pd.Timedelta(days=int(ndays))
126126

127127
# Filter data to the specified date range

tests/test_active_percent.py

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ def test_active_percent_iglu_r_compatible(scenario):
7070
)
7171

7272

73-
def test_active_percent_output_format():
74-
"""Test the output format of active_percent function"""
75-
76-
# Create test data with known gaps
73+
def test_active_percent_basic_output():
74+
"""Test basic output format and structure of active_percent function"""
7775
data = pd.DataFrame(
7876
{
7977
"id": [
@@ -98,7 +96,6 @@ def test_active_percent_output_format():
9896
}
9997
)
10098

101-
# Test with default parameters
10299
result = iglu.active_percent(data)
103100

104101
# Check DataFrame structure
@@ -107,32 +104,78 @@ def test_active_percent_output_format():
107104
col in result.columns
108105
for col in ["id", "active_percent", "ndays", "start_date", "end_date"]
109106
)
110-
111-
# Check values are between 0 and 100
112107
assert all((result["active_percent"] >= 0) & (result["active_percent"] <= 100))
113-
114-
# Check ndays is non-negative
115108
assert all(result["ndays"] >= 0)
116109

117-
# Test with custom dt0
118-
result_custom = iglu.active_percent(data, dt0=5)
119-
assert isinstance(result_custom, pd.DataFrame)
110+
def test_active_percent_custom_dt0():
111+
"""Test active_percent with custom dt0 parameter"""
112+
data = pd.DataFrame(
113+
{
114+
"id": ["subject1"] * 4,
115+
"time": pd.to_datetime(
116+
[
117+
"2020-01-01 00:00:00",
118+
"2020-01-01 00:05:00",
119+
"2020-01-01 00:15:00",
120+
"2020-01-01 00:20:00",
121+
]
122+
),
123+
"gl": [150, np.nan, 160, 165],
124+
}
125+
)
126+
127+
result = iglu.active_percent(data, dt0=5)
128+
assert isinstance(result, pd.DataFrame)
129+
130+
def test_active_percent_consistent_end_date():
131+
"""Test active_percent with consistent end date"""
132+
data = pd.DataFrame(
133+
{
134+
"id": ["subject1"] * 4,
135+
"time": pd.to_datetime(
136+
[
137+
"2020-01-01 00:00:00",
138+
"2020-01-01 00:05:00",
139+
"2020-01-01 00:15:00",
140+
"2020-01-01 00:20:00",
141+
]
142+
),
143+
"gl": [150, np.nan, 160, 165],
144+
}
145+
)
120146

121-
# Test with consistent end date
122147
end_date = datetime(2020, 1, 1, 1, 0) # 1 hour after start
123-
result_consistent = iglu.active_percent(data, consistent_end_date=end_date)
124-
assert all(result_consistent["end_date"] == end_date)
148+
result = iglu.active_percent(data, consistent_end_date=end_date)
149+
assert all(result["end_date"].dt.tz_localize(None) == pd.to_datetime(end_date).tz_localize(None))
150+
151+
def test_active_percent_timezone():
152+
"""Test active_percent with timezone parameter"""
153+
data = pd.DataFrame(
154+
{
155+
"id": ["subject1"] * 4,
156+
"time": pd.to_datetime(
157+
[
158+
"2020-01-01 00:00:00",
159+
"2020-01-01 00:05:00",
160+
"2020-01-01 00:15:00",
161+
"2020-01-01 00:20:00",
162+
]
163+
),
164+
"gl": [150, np.nan, 160, 165],
165+
}
166+
)
125167

126-
# Test with timezone
127-
result_tz = iglu.active_percent(data, tz="GMT")
128-
assert isinstance(result_tz, pd.DataFrame)
168+
result = iglu.active_percent(data, tz="GMT")
169+
assert isinstance(result, pd.DataFrame)
129170

130-
# Test with empty data
171+
def test_active_percent_empty_data():
172+
"""Test active_percent with empty DataFrame"""
131173
empty_data = pd.DataFrame(columns=["id", "time", "gl"])
132174
with pytest.raises(ValueError):
133175
iglu.active_percent(empty_data)
134176

135-
# Test with single subject and no gaps
177+
def test_active_percent_single_subject_no_gaps():
178+
"""Test active_percent with single subject and no gaps"""
136179
single_subject = pd.DataFrame(
137180
{
138181
"id": ["subject1"] * 3,
@@ -142,8 +185,7 @@ def test_active_percent_output_format():
142185
"gl": [150, 155, 160],
143186
}
144187
)
145-
result_single = iglu.active_percent(single_subject, dt0=5)
146-
assert len(result_single) == 1
147-
assert (
148-
result_single["active_percent"].iloc[0] == 100.0
149-
) # Should be 100% active with no gaps
188+
189+
result = iglu.active_percent(single_subject, dt0=5)
190+
assert len(result) == 1
191+
assert result["active_percent"].iloc[0] == 100.0 # Should be 100% active with no gaps

0 commit comments

Comments
 (0)