Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to plotting tool #240

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/accelerometer/accPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ def main(): # noqa: C901

# read time series file to pandas DataFrame
data = pd.read_csv(
args.timeSeriesFile, index_col='time',
parse_dates=['time'], date_parser=utils.date_parser
args.timeSeriesFile,
index_col='time',
parse_dates=['time'],
date_parser=utils.date_parser
)

# set backend if run from main
Expand All @@ -92,16 +94,16 @@ def main(): # noqa: C901


def plotTimeSeries( # noqa: C901
data,
title=None,
showFirstNDays=None
):
data,
title=None,
showFirstNDays=None
):
"""Plot overall activity and classified activity types

:param pd.DataFrame data: Input DataFrame with time series data
Index: DatetimeIndex
Columns (4 class example):
Name: acc, dtype=float
Name: acc, dtype=float (optional)
Name: light, dtype=Any numeric, value=0 or 1
Name: moderate-vigorous, dtype=Any numeric, value=0 or 1
Name: sedentary, dtype=Any numeric, value=0 or 1
Expand All @@ -119,9 +121,20 @@ def plotTimeSeries( # noqa: C901
>>> fig.show()
"""

# double check time index
if 'time' in data.columns:
data = data.set_index('time')
# check index is datetime
if not isinstance(data.index, pd.DatetimeIndex):
raise ValueError("Index must be a DatetimeIndex")

# use tz-naive local time
if data.index.tz is not None:
data.index = (
# convoluted way to mask the ambiguous DST pushback hour, if any
data.index
.tz_localize(None)
.tz_localize(data.index.tz, ambiguous='NaT', nonexistent='NaT')
.tz_localize(None)
)
data = data[data.index.notnull()]

# fix gaps or irregular sampling
if pd.infer_freq(data) is None:
Expand All @@ -143,12 +156,12 @@ def plotTimeSeries( # noqa: C901

# setup plotting range
MAXRANGE = 2 * 1000 # 2g (above this is very rare)
data['acc'] = data['acc'].rolling('1T').mean() # minute average
data['acc'] = data['acc'].clip(0, MAXRANGE)
if 'acc' in data:
data['acc'] = data['acc'].rolling('1T').mean() # minute average
data['acc'] = data['acc'].clip(0, MAXRANGE)
data[labels] = data[labels].astype('f4') * MAXRANGE

# number of rows to display in figure (all days + legend)
data.index = data.index.tz_localize(None, ambiguous='NaT', nonexistent='NaT') # tz-unaware local time
groupedDays = data.groupby(data.index.date)
nrows = len(groupedDays) + 1

Expand All @@ -164,7 +177,8 @@ def plotTimeSeries( # noqa: C901

ax = fig.add_subplot(nrows, 1, i + 1)

ax.plot(group.index, group['acc'].to_numpy(), c='k')
if 'acc' in group:
ax.plot(group.index, group['acc'].to_numpy(), c='k')

if len(labels) > 0:
ax.stackplot(group.index,
Expand Down
Loading