Skip to content

Commit

Permalink
fix: peak cadence to nan when not enough data
Browse files Browse the repository at this point in the history
  • Loading branch information
chanshing committed Apr 11, 2024
1 parent 8393f86 commit 90c60f7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/stepcount/stepcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,22 @@ def _sum(x):
return np.nansum(x)
return np.sum(x)

def _max(x, n=1):
if skipna:
return x.nlargest(n, keep='all').mean()
elif x.isna().any():
return np.nan

# there's a bug with .resample().sum(skipna)
# https://github.com/pandas-dev/pandas/issues/29382

# steps
total = np.round(Y.agg(_sum)) # total steps
hourly = Y.resample('H').agg(_sum).round().rename('Steps') # steps, hourly
daily = Y.resample('D').agg(_sum).round().rename('Steps') # steps, daily
minutely = Y.resample('T').agg(_sum).round().rename('Steps') # steps, minutely

# steps, daily stats
if not adjust_estimates:
daily_avg = np.round(daily.mean())
daily_med = np.round(daily.median())
Expand All @@ -186,6 +195,8 @@ def _sum(x):
W = Y.mask(~Y.isna(), Y >= steptol)
total_walk = np.round(W.agg(_sum) * dt / 60)
daily_walk = (W.resample('D').agg(_sum) * dt / 60).round().rename('Walk(mins)')

# walking, daily stats
if not adjust_estimates:
daily_walk_avg = np.round(daily_walk.mean())
daily_walk_med = np.round(daily_walk.median())
Expand All @@ -198,13 +209,9 @@ def _sum(x):
daily_walk_min = np.round(weekdaily_walk.min())
daily_walk_max = np.round(weekdaily_walk.max())

def _max(x, n=1):
return x.nlargest(n, keep='all').mean()

# cadence https://jamanetwork.com/journals/jama/fullarticle/2763292
cadence = Y.resample('min').sum()
daily_cadence_peak1 = cadence.resample('D').agg(_max, n=1)
daily_cadence_peak30 = cadence.resample('D').agg(_max, n=30)
daily_cadence_peak1 = minutely.resample('D').agg(_max, n=1)
daily_cadence_peak30 = minutely.resample('D').agg(_max, n=30)
if not adjust_estimates:
cadence_peak1 = np.round(daily_cadence_peak1.mean())
cadence_peak30 = np.round(daily_cadence_peak30.mean())
Expand Down

0 comments on commit 90c60f7

Please sign in to comment.