Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.provider": "black",
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
Expand All @@ -38,7 +39,8 @@
"ms-python.python",
"ms-python.vscode-pylance",
"ms-azuretools.vscode-docker",
"ms-toolsai.jupyter"
"ms-toolsai.jupyter",
"charliermarsh.ruff"
]
}
},
Expand Down
51 changes: 4 additions & 47 deletions scripts/basics/basic_setup.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,10 @@
#%%
# %%
from autodyn.core import dynamical as dyn
from autodyn.models.canonical.standard import (
lorenz,
consensus,
single_hopf,
controlled_hopf,
)
from autodyn.models.canonical.standard import lorenz
from autodyn.core.network import connectivity
import numpy as np

#%%
# %%
lorenz_sys = dyn.system(lorenz, D=3)
lorenz_sys.simulate(T=50, dt=0.01, sigma=10, rho=28, beta=8 / 3)
lorenz_sys.plot_phase()


#%%
test_sys = dyn.system(consensus, D=10)
brain_net = connectivity(10, proportion=0.4)

test_sys.simulate(T=100, dt=0.1, D=brain_net.D.T)

test_sys.plot_phase()

brain_net.plot_incidence()
brain_net.plot_spectrum()
#%%
# Design our stimulation waveform here


times = 50
dt = 0.01
stim = np.zeros((int(times // dt) + 1, 1))
stim[stim.shape[0] // 2 :: 10] = 40

# Setup and run our dynamics
hopf_single = dyn.system(controlled_hopf, D=2)
hopf_single.simulate(
T=times,
dt=dt,
sigma=10,
rho=28,
beta=8 / 3,
c=3,
w=1,
g=1,
stim=stim,
keep_positive=True,
)
hopf_single.plot_polar()

# %%
lorenz_sys.plot_phase(d1=0, d2=1)
8 changes: 8 additions & 0 deletions scripts/basics/builder_dynamics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from autodyn.builder import microstruct, macrostruct
import networkx as nx

# %%
neural_mass = microstruct()
brain_network = macrostruct(L=brain_graph)

main_system = microstruct + macrostruct
13 changes: 13 additions & 0 deletions scripts/basics/sys_types/consensus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from autodyn.core import dynamical as dyn
from autodyn.models.canonical.standard import consensus
from autodyn.core.network import connectivity

# %%
test_sys = dyn.system(consensus, D=10)
brain_net = connectivity(10, proportion=0.4)

test_sys.simulate(T=100, dt=0.1, D=brain_net.D.T)
test_sys.plot_phase(d1=0, d2=2)

brain_net.plot_incidence()
brain_net.plot_spectrum()
27 changes: 27 additions & 0 deletions scripts/basics/sys_types/controlled_hopf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# %%
import numpy as np
from autodyn.core import dynamical as dyn
from autodyn.models.canonical.standard import controlled_hopf

# %%
# Design our stimulation waveform here
times = 50
dt = 0.01
stim = np.zeros((int(times // dt) + 1, 1))
stim[stim.shape[0] // 2 :: 10] = 40

# Setup and run our dynamics
hopf_single = dyn.system(controlled_hopf, D=2)
hopf_single.simulate(
T=times,
dt=dt,
sigma=10,
rho=28,
beta=8 / 3,
c=3,
w=1,
g=1,
stim=stim,
keep_positive=True,
)
hopf_single.plot_polar()
11 changes: 11 additions & 0 deletions scripts/basics/sys_types/kuramoto_eg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# %%
import numpy as np
from autodyn.core import dynamical as dyn
from autodyn.models.neuro.scalar import kuramoto

# %%
kmo = dyn.system(kuramoto, D=3)
param_set = {"D": 1 * np.eye(3), "w": np.pi / 2}
kmo.simulate(T=100, dt=0.1, params=param_set)
kmo.plot_phase(0, 1)
kmo.plot_polar()
1 change: 0 additions & 1 deletion scripts/wilson-cowan/basic_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
T = 100
dt = 0.1
tpts = int(T // dt) + 1

tvect = np.linspace(0, T, tpts)

u = np.zeros_like(tvect)
Expand Down
35 changes: 35 additions & 0 deletions scripts/wilson-cowan/neurotransmitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#%%
from autodyn.core import dynamical as dyn
from autodyn.models.neuro.wc import wc_drift, wc_input
from autodyn.core import control

#%%
wilson_cowan = dyn.system(wc_drift, D=2)
param_set = {
"T_e": 5,
"T_i": 5,
"beta": {"e": -1, "i": -1},
"w": {"ee": 10, "ii": 3, "ei": 12, "ie": 8},
"alpha": 0.1,
"thresh": {"e": 0.2, "i": 4},
"tau": 0,
"net_k": 1 / 10,
}
wilson_cowan.simulate(T=100, dt=0.1, params=param_set)
wilson_cowan.plot_phase()

#%%
def step_u(x):
_, t = x.shape
u = np.zero_like(x)
u[t // 2 :: 2] = 1

return u


controller = control(u=step_u)


wilson_cowan = dyn.system(wc_input, D=2)
wilson_cowan.simulate(T=T, dt=dt, params=param_set, stim=u)
wilson_cowan.plot_phase()
Empty file added src/autodyn/builder/__init__.py
Empty file.
Empty file added src/autodyn/builder/macro.py
Empty file.
22 changes: 22 additions & 0 deletions src/autodyn/builder/micro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass
from typing import Optional, Callable
import numpy as np
from autodyn.core.network import connectivity
import networkx as nx
from autodyn.utils.functions import unity


class microstruct:
def __init__(self):
pass

@property
def dynamics(self):
if not self._dynamics:
self._dynamics = unity

return self._dynamics

@dynamics.setter
def dynamics(self, f: Callable):
self._dynamics = f
3 changes: 3 additions & 0 deletions src/autodyn/core/control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class control:
def __init__(self):
pass
36 changes: 25 additions & 11 deletions src/autodyn/core/dynamical.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 11 17:21:34 2020

@author: virati
Barebones class for dynamical systems
"""
from typing import Optional

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import scipy.signal as sig
from autodyn.utils.functions import unity
from autodyn.core import control

from autodyn.core.integrators.runge_kutta import rk_integrator

Expand All @@ -21,10 +14,26 @@ def __init__(self, f, D: int = 3, net_graph: nx.Graph = None):
self.x = np.zeros((D, 1))
self.D = D
self.f = f
self.u_func: Optional[callable] = None

self.gen_connectivity()
self.post_step = unity

@property
def u(self):
self._u: control
if self.u_func is None:
return np.zeros_like(self.x)

if not self._u:
self._u = control(self.u_func)
return self._u

@u.setter
def u(self, value: np.ndarray):
self._u = control()
self._u.raw = value

def set_post_step(self, func: callable):
self.post_step = func

Expand Down Expand Up @@ -83,7 +92,12 @@ def plot_raster(self):
plt.plot(self.raster)
plt.show()

def plot_phase(self):
def plot_phase(self, d1=0, d2=1):
fig = plt.figure()
plt.plot(self.raster[:, d1], self.raster[:, d2])
plt.title("Phase")

def plot_phase_full(self):
if self.D == 3:
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
Expand All @@ -103,4 +117,4 @@ def plot_measure(self):
def plot_polar(self):
plt.figure()
plt.plot(np.real(self.raster[:, 0] * np.exp(1j * self.raster[:, 1])))
plt.title("Measured Trajectories in Time")
plt.title("Polar Trajectories in Time")
Empty file.
24 changes: 24 additions & 0 deletions src/autodyn/core/lifters/legendre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Optional
import jax.numpy as np
import numpy as nnp


def L_lift(x):

a * x + a * x**3


def lift(
trajectory: np.ndarray,
M: int,
U: Optional[np.ndarray] = None,
):
if trajectory.ndim != 2:
raise NotImplemented

N, T = trajectory.shape
if U is None:
U = nnp.random.multivariate_normal(nnp.zeros(M), nnp.eye(M, N), size=(M, N))

# need an array of callables
U @ f(trajectory)
Empty file added src/autodyn/models/neuro/jr.py
Empty file.
14 changes: 14 additions & 0 deletions src/autodyn/models/neuro/scalar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from numpy import cos


def kuramoto(x, g=None, u=0, **kwargs):
D = kwargs["params"]["D"]
w = kwargs["params"]["w"]

new_x = w - D @ cos(D.T @ x)

return new_x


def delay_kuramoto(x, g=None, u=0, **kwargs):
raise NotImplemented