Skip to content

Commit 902caa4

Browse files
committed
extend tutorial_4 and implement plot method within the model class
1 parent bb91de9 commit 902caa4

4 files changed

Lines changed: 65591 additions & 38 deletions

File tree

circStudio/analysis/models/math_models.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from scipy.integrate import odeint
55
from scipy.optimize import curve_fit
66
import matplotlib.pyplot as plt
7+
import plotly.graph_objs as go
78

89

910
class Model:
@@ -186,6 +187,63 @@ def dlmos(self):
186187
return self.cbt() - self.cbt_to_dlmo
187188

188189

190+
def plot(self, states=False, dlmo=False, cbtmin=False):
191+
# Create a new plotly figure
192+
fig = go.Figure()
193+
194+
if states:
195+
# Calculate number of states available
196+
states = self.model_states.shape[1]
197+
198+
if self.model_name == 'Forger' or self.model_name == 'Jewett':
199+
labels = ['x','xc', 'Light Drive']
200+
elif self.model_name == 'HannaySP':
201+
labels = ['Amplitude','Phase', 'Light Drive']
202+
else:
203+
labels = ['Ventral Amplitude', 'Dorsal Amplitude', 'Ventral Phase', 'Dorsal Phase', 'Light Drive']
204+
# Iterate over states and plot them
205+
for i in range(states):
206+
fig.add_trace(go.Scatter(
207+
x=self.data.index.astype(str),
208+
y=self.model_states[:, i],
209+
name=f'{labels[i]}',
210+
))
211+
fig.update_layout(
212+
title='Model States',
213+
xaxis=dict(title='Time'),
214+
yaxis=dict(title='Model States'),
215+
)
216+
return fig
217+
218+
if dlmo:
219+
# Plot daily predicted DLMO
220+
fig.add_trace(go.Scatter(
221+
x=pd.Series(self.data.index.date.astype(str)).unique(),
222+
y=self.dlmos() % 24,
223+
name='Predicted DLMO',
224+
))
225+
fig.update_layout(
226+
title='Predicted DLMO',
227+
xaxis=dict(title='Day'),
228+
yaxis=dict(title='DLMO time'),
229+
)
230+
return fig
231+
232+
if cbtmin:
233+
# Plot daily predicted DLMO
234+
fig.add_trace(go.Scatter(
235+
x=pd.Series(self.data.index.date.astype(str)).unique(),
236+
y=self.cbt() % 24,
237+
name='Predicted CBTmin',
238+
))
239+
fig.update_layout(
240+
title='Predicted CBTmin',
241+
xaxis=dict(title='Day'),
242+
yaxis=dict(title='CBTmin time'),
243+
)
244+
return fig
245+
246+
189247
class Forger(Model):
190248
"""
191249
Implements the mathematical model of human circadian rhythms developed by Forger, Jewett and Kronauer [1].
@@ -280,6 +338,7 @@ def __init__(
280338
# self.initial_conditions = np.array([-0.0843259, -1.09607546, 0.45584306])
281339
# self.inputs = inputs
282340
# self.time = time
341+
self.model_name = self.__class__.__name__
283342
self.taux = taux
284343
self.mu = mu
285344
self.g = g
@@ -484,6 +543,7 @@ def __init__(
484543
# self.initial_conditions= np.array([-0.10097101, -1.21985662, 0.50529415])
485544
# self.inputs = inputs
486545
# self.time = time
546+
self.model_name = self.__class__.__name__
487547
self.taux = taux
488548
self.mu = mu
489549
self.g = g
@@ -697,6 +757,7 @@ def __init__(
697757
# self.initial_conditions = np.array([0.82041911, 1.71383697, 0.52318122])
698758
# self.inputs = inputs
699759
# self.time = time
760+
self.model_name = self.__class__.__name__
700761
self.tau = tau
701762
self.k = k
702763
self.gamma = gamma
@@ -947,6 +1008,7 @@ def __init__(
9471008
# self.initial_conditions = np.array([0.82423745, 0.82304996, 1.75233424, 1.863457, 0.52318122])
9481009
# self.inputs = inputs
9491010
# self.time = time
1011+
self.model_name = self.__class__.__name__
9501012
self.tauv = tauv
9511013
self.taud = taud
9521014
self.kvv = kvv
@@ -1166,8 +1228,8 @@ def __init__(
11661228
# Extract time and light vector
11671229
if time is None or inputs is None:
11681230
if data is not None:
1169-
self.time = np.asarray((data.index - data.index.min()).total_seconds() / 3600)
1170-
self.inputs = np.asarray(data.values)
1231+
self.time_vector = np.asarray((data.index - data.index.min()).total_seconds() / 3600)
1232+
self.light_vector = np.asarray(data.values)
11711233
else:
11721234
raise ValueError("Must provide either light time series (data) or input and time.")
11731235
else:

0 commit comments

Comments
 (0)