Skip to content

Commit 6ed8e31

Browse files
committed
Update tutorial_3.ipynb and daily_profile
1 parent 56ef748 commit 6ed8e31

5 files changed

Lines changed: 6768 additions & 27386 deletions

File tree

circStudio/analysis/metrics/metrics.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import numpy as np
33
import re
44
from circStudio.analysis.tools import *
5-
from statistics import mean
6-
import statsmodels.api as sm
5+
import plotly.graph_objects as go
76

87

9-
def daily_profile(data, cyclic=False, time_origin=None, whs="1h"):
8+
def daily_profile(data, cyclic=False, time_origin=None, whs="1h", plot=False, log=False):
109
r"""Average daily activity/light/temperature distribution
1110
1211
Calculate the daily profile of activity. Data are averaged over all the
@@ -33,15 +32,58 @@ def daily_profile(data, cyclic=False, time_origin=None, whs="1h"):
3332
onset/offset time. Relevant only if time_origin is set to
3433
'AonT' or AoffT'.
3534
Default is '1h'.
35+
plot: bool, optional
36+
Whether to plot the daily profile. Default is False.
37+
log: bool, optional
38+
Whether the daily profile should be transformed to a log10 scale.
3639
3740
Returns
3841
-------
3942
raw : pandas.Series
4043
A Series containing the daily activity profile with a 24h index.
4144
"""
42-
if time_origin is None:
43-
return _average_daily_activity(data, cyclic=cyclic)
45+
def _format(to_plot, to_log, profile):
46+
"""
47+
Internal function that decides whether to present daily profile as pd.Series
48+
or as an interactive Plotly figure.
49+
50+
Parameters
51+
----------
52+
to_plot : bool
53+
Whether the daily profile should be plotted.
54+
to_log : bool
55+
Whether the daily profile should be transformed to a log10 scale.
56+
profile
57+
The daily profile returned by this function.
58+
59+
Returns
60+
-------
61+
pandas.Series or go.Figure
62+
63+
"""
64+
if to_plot:
65+
layout = go.Layout(
66+
title="Daily profile",
67+
xaxis=dict(title="Date time"),
68+
yaxis=dict(title="Data"),
69+
showlegend=False
70+
)
71+
if to_log:
72+
fig = go.Figure(data=[
73+
go.Scatter(x=profile.index.astype(str), y=np.log10(profile+1))
74+
], layout=layout)
75+
return fig
76+
else:
77+
fig = go.Figure(data=[
78+
go.Scatter(x=profile.index.astype(str), y=profile)
79+
], layout=layout)
80+
return fig
81+
else:
82+
return profile
4483

84+
if time_origin is None:
85+
_daily_profile = _average_daily_activity(data, cyclic=cyclic)
86+
return _format(plot, log, _daily_profile)
4587
else:
4688
if cyclic is True:
4789
raise NotImplementedError(
@@ -85,7 +127,8 @@ def daily_profile(data, cyclic=False, time_origin=None, whs="1h"):
85127

86128
shift = int((pd.Timedelta("12h") - time_origin) / data.index.freq)
87129

88-
return _shift_time_axis(avgdaily, shift)
130+
_daily_profile = _shift_time_axis(avgdaily, shift)
131+
return _format(plot, log, _daily_profile)
89132

90133

91134
def daily_profile_auc(data, start_time=None, stop_time=None, time_origin=None):
@@ -252,7 +295,7 @@ def l5(data):
252295
253296
Returns
254297
-------
255-
l5: float
298+
l5_onset, l5 : float
256299
257300
Notes
258301
-----
@@ -291,7 +334,7 @@ def m10(data):
291334
292335
Returns
293336
-------
294-
m10: float
337+
m10_onset, m10 : float
295338
296339
Notes
297340
-----

docs/source/notebooks/legacy_notebooks/03 - Masking spurious inactivity.ipynb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113515,9 +113515,7 @@
113515113515
},
113516113516
{
113517113517
"cell_type": "markdown",
113518-
"metadata": {
113519-
"jp-MarkdownHeadingCollapsed": true
113520-
},
113518+
"metadata": {},
113521113519
"source": [
113522113520
"### Apply the mask"
113523113521
]

docs/source/notebooks/legacy_notebooks/04 - Calculating rest-activity variables.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@
4848
},
4949
{
5050
"cell_type": "code",
51-
"execution_count": 1,
5251
"metadata": {
5352
"ExecuteTime": {
54-
"end_time": "2025-07-10T20:00:36.195856Z",
55-
"start_time": "2025-07-10T20:00:34.750504Z"
53+
"end_time": "2025-07-22T14:31:48.659072Z",
54+
"start_time": "2025-07-22T14:31:47.468503Z"
5655
}
5756
},
58-
"outputs": [],
5957
"source": [
6058
"import circStudio"
61-
]
59+
],
60+
"outputs": [],
61+
"execution_count": 1
6262
},
6363
{
6464
"cell_type": "code",

docs/source/notebooks/legacy_notebooks/05 - Visualizing sleep diaries.ipynb

Lines changed: 36 additions & 27370 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)