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

moved computedelaymatrix function to /utils/model_utils #235

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 0 additions & 21 deletions neurolib/models/aln/loadDefaultParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,6 @@ def loadDefaultParams(Cmat=None, Dmat=None, lookupTableFileName=None, seed=None)
return params


def computeDelayMatrix(lengthMat, signalV, segmentLength=1):
"""
Compute the delay matrix from the fiber length matrix and the signal
velocity

:param lengthMat: A matrix containing the connection length in
segment
:param signalV: Signal velocity in m/s
:param segmentLength: Length of a single segment in mm

:returns: A matrix of connexion delay in ms
"""

normalizedLenMat = lengthMat * segmentLength
if signalV > 0:
Dmat = normalizedLenMat / signalV # Interareal delays in ms
else:
Dmat = lengthMat * 0.0
return Dmat


def generateRandomICs(N, seed=None):
"""Generates random Initial Conditions for the interareal network

Expand Down
34 changes: 17 additions & 17 deletions neurolib/models/aln/timeIntegration.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
import numba

from . import loadDefaultParams as dp
from ...utils import model_utils as mu


def timeIntegration(params):
"""Sets up the parameters for time integration

Return:
rates_exc: N*L array : containing the exc. neuron rates in kHz time series of the N nodes
rates_inh: N*L array : containing the inh. neuron rates in kHz time series of the N nodes
Expand Down Expand Up @@ -53,7 +52,7 @@ def timeIntegration(params):
if N == 1:
Dmat = np.ones((N, N)) * params["de"]
else:
Dmat = dp.computeDelayMatrix(
Dmat = mu.computeDelayMatrix(
lengthMat, signalV
) # Interareal connection delays, Dmat(i,j) Connnection from jth node to ith (ms)
Dmat[np.eye(len(Dmat)) == 1] = np.ones(len(Dmat)) * params["de"]
Expand Down Expand Up @@ -399,10 +398,10 @@ def timeIntegration_njit_elementwise(
):

# squared Jee_max
sq_Jee_max = Jee_max ** 2
sq_Jei_max = Jei_max ** 2
sq_Jie_max = Jie_max ** 2
sq_Jii_max = Jii_max ** 2
sq_Jee_max = Jee_max**2
sq_Jei_max = Jei_max**2
sq_Jie_max = Jie_max**2
sq_Jii_max = Jii_max**2

# initialize so we don't get an error when returning
rd_exc_rhs = 0.0
Expand Down Expand Up @@ -456,24 +455,24 @@ def timeIntegration_njit_elementwise(
z1ii = cii * Ki * rd_inh[no]
# z2: weighted sum of delayed rates, weights=c^2*K (see thesis last ch.)
z2ee = (
cee ** 2 * Ke * rd_exc[no, no] + c_gl ** 2 * Ke_gl * rowsumsq + c_gl ** 2 * Ke_gl * ext_exc_rate[no, i]
cee**2 * Ke * rd_exc[no, no] + c_gl**2 * Ke_gl * rowsumsq + c_gl**2 * Ke_gl * ext_exc_rate[no, i]
)
z2ei = cei ** 2 * Ki * rd_inh[no]
z2ei = cei**2 * Ki * rd_inh[no]
z2ie = (
cie ** 2 * Ke * rd_exc[no, no] + c_gl ** 2 * Ke_gl * ext_inh_rate[no, i]
cie**2 * Ke * rd_exc[no, no] + c_gl**2 * Ke_gl * ext_inh_rate[no, i]
) # external rate input to inh. population
z2ii = cii ** 2 * Ki * rd_inh[no]
z2ii = cii**2 * Ki * rd_inh[no]

sigmae = np.sqrt(
2 * sq_Jee_max * seev[no] * tau_se * taum / ((1 + z1ee) * taum + tau_se)
+ 2 * sq_Jei_max * seiv[no] * tau_si * taum / ((1 + z1ei) * taum + tau_si)
+ sigmae_ext ** 2
+ sigmae_ext**2
) # mV/sqrt(ms)

sigmai = np.sqrt(
2 * sq_Jie_max * siev[no] * tau_se * taum / ((1 + z1ie) * taum + tau_se)
+ 2 * sq_Jii_max * siiv[no] * tau_si * taum / ((1 + z1ii) * taum + tau_si)
+ sigmai_ext ** 2
+ sigmai_ext**2
) # mV/sqrt(ms)

if not filter_sigma:
Expand Down Expand Up @@ -531,10 +530,10 @@ def timeIntegration_njit_elementwise(
seim_rhs = ((1 - seim[no]) * z1ei - seim[no]) / tau_si
siem_rhs = ((1 - siem[no]) * z1ie - siem[no]) / tau_se
siim_rhs = ((1 - siim[no]) * z1ii - siim[no]) / tau_si
seev_rhs = ((1 - seem[no]) ** 2 * z2ee + (z2ee - 2 * tau_se * (z1ee + 1)) * seev[no]) / tau_se ** 2
seiv_rhs = ((1 - seim[no]) ** 2 * z2ei + (z2ei - 2 * tau_si * (z1ei + 1)) * seiv[no]) / tau_si ** 2
siev_rhs = ((1 - siem[no]) ** 2 * z2ie + (z2ie - 2 * tau_se * (z1ie + 1)) * siev[no]) / tau_se ** 2
siiv_rhs = ((1 - siim[no]) ** 2 * z2ii + (z2ii - 2 * tau_si * (z1ii + 1)) * siiv[no]) / tau_si ** 2
seev_rhs = ((1 - seem[no]) ** 2 * z2ee + (z2ee - 2 * tau_se * (z1ee + 1)) * seev[no]) / tau_se**2
seiv_rhs = ((1 - seim[no]) ** 2 * z2ei + (z2ei - 2 * tau_si * (z1ei + 1)) * seiv[no]) / tau_si**2
siev_rhs = ((1 - siem[no]) ** 2 * z2ie + (z2ie - 2 * tau_se * (z1ie + 1)) * siev[no]) / tau_se**2
siiv_rhs = ((1 - siim[no]) ** 2 * z2ii + (z2ii - 2 * tau_si * (z1ii + 1)) * siiv[no]) / tau_si**2

# -------------- integration --------------

Expand Down Expand Up @@ -636,6 +635,7 @@ def lookup_no_interp(x, dx, xi, y, dy, yi):

return idxX, idxY


@numba.njit(locals={"xid1": numba.int64, "yid1": numba.int64, "dxid": numba.float64, "dyid": numba.float64})
def fast_interp2_opt(x, dx, xi, y, dy, yi):

Expand Down
19 changes: 0 additions & 19 deletions neurolib/models/fhn/loadDefaultParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,3 @@ def loadDefaultParams(Cmat=None, Dmat=None, seed=None):
params.y_ext = np.zeros((params.N,))

return params


def computeDelayMatrix(lengthMat, signalV, segmentLength=1):
"""Compute the delay matrix from the fiber length matrix and the signal velocity

:param lengthMat: A matrix containing the connection length in segment
:param signalV: Signal velocity in m/s
:param segmentLength: Length of a single segment in mm

:returns: A matrix of connexion delay in ms
"""

normalizedLenMat = lengthMat * segmentLength
# Interareal connection delays, Dmat(i,j) in ms
if signalV > 0:
Dmat = normalizedLenMat / signalV
else:
Dmat = lengthMat * 0.0
return Dmat
9 changes: 4 additions & 5 deletions neurolib/models/fhn/timeIntegration.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
import numba

from . import loadDefaultParams as dp
from ...utils import model_utils as mu


def timeIntegration(params):
"""Sets up the parameters for time integration

:param params: Parameter dictionary of the model
:type params: dict
:return: Integrated activity variables of the model
Expand Down Expand Up @@ -53,7 +52,7 @@ def timeIntegration(params):
Dmat = np.zeros((N, N))
else:
# Interareal connection delays, Dmat(i,j) Connnection from jth node to ith (ms)
Dmat = dp.computeDelayMatrix(lengthMat, signalV)
Dmat = mu.computeDelayMatrix(lengthMat, signalV)
# no self-feedback delay
Dmat[np.eye(len(Dmat)) == 1] = np.zeros(len(Dmat))
Dmat_ndt = np.around(Dmat / dt).astype(int) # delay matrix in multiples of dt
Expand Down Expand Up @@ -229,13 +228,13 @@ def timeIntegration_njit_elementwise(
- ys[no, i - 1]
+ xs_input_d[no] # input from other nodes
+ x_ou[no] # ou noise
+ x_ext[no, i-1] # external input
+ x_ext[no, i - 1] # external input
)
y_rhs = (
(xs[no, i - 1] - delta - epsilon * ys[no, i - 1]) / tau
+ ys_input_d[no] # input from other nodes
+ y_ou[no] # ou noise
+ y_ext[no, i-1] # external input
+ y_ext[no, i - 1] # external input
)

# Euler integration
Expand Down
19 changes: 0 additions & 19 deletions neurolib/models/hopf/loadDefaultParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,3 @@ def loadDefaultParams(Cmat=None, Dmat=None, seed=None):
params.y_ext = np.zeros((params.N,))

return params


def computeDelayMatrix(lengthMat, signalV, segmentLength=1):
"""Compute the delay matrix from the fiber length matrix and the signal velocity

:param lengthMat: A matrix containing the connection length in segment
:param signalV: Signal velocity in m/s
:param segmentLength: Length of a single segment in mm

:returns: A matrix of connexion delay in ms
"""

normalizedLenMat = lengthMat * segmentLength
# Interareal connection delays, Dmat(i,j) in ms
if signalV > 0:
Dmat = normalizedLenMat / signalV
else:
Dmat = lengthMat * 0.0
return Dmat
9 changes: 4 additions & 5 deletions neurolib/models/hopf/timeIntegration.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
import numba

from . import loadDefaultParams as dp
from ...utils import model_utils as mu


def timeIntegration(params):
"""Sets up the parameters for time integration

:param params: Parameter dictionary of the model
:type params: dict
:return: Integrated activity variables of the model
Expand Down Expand Up @@ -59,7 +58,7 @@ def timeIntegration(params):
Dmat = np.zeros((N, N))
else:
# Interareal connection delays, Dmat(i,j) Connnection from jth node to ith (ms)
Dmat = dp.computeDelayMatrix(lengthMat, signalV)
Dmat = mu.computeDelayMatrix(lengthMat, signalV)
Dmat[np.eye(len(Dmat)) == 1] = np.zeros(len(Dmat))
Dmat_ndt = np.around(Dmat / dt).astype(int) # delay matrix in multiples of dt
params["Dmat_ndt"] = Dmat_ndt
Expand Down Expand Up @@ -208,14 +207,14 @@ def timeIntegration_njit_elementwise(
- w * ys[no, i - 1]
+ xs_input_d[no] # input from other nodes
+ x_ou[no] # ou noise
+ x_ext[no, i-1] # external input
+ x_ext[no, i - 1] # external input
)
y_rhs = (
(a - xs[no, i - 1] ** 2 - ys[no, i - 1] ** 2) * ys[no, i - 1]
+ w * xs[no, i - 1]
+ ys_input_d[no] # input from other nodes
+ y_ou[no] # ou noise
+ y_ext[no, i-1] # external input
+ y_ext[no, i - 1] # external input
)

# Euler integration
Expand Down
19 changes: 0 additions & 19 deletions neurolib/models/wc/loadDefaultParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,3 @@ def loadDefaultParams(Cmat=None, Dmat=None, seed=None):
params.inh_ou = np.zeros((params.N,))

return params


def computeDelayMatrix(lengthMat, signalV, segmentLength=1):
"""Compute the delay matrix from the fiber length matrix and the signal velocity

:param lengthMat: A matrix containing the connection length in segment
:param signalV: Signal velocity in m/s
:param segmentLength: Length of a single segment in mm

:returns: A matrix of connexion delay in ms
"""

normalizedLenMat = lengthMat * segmentLength
# Interareal connection delays, Dmat(i,j) in ms
if signalV > 0:
Dmat = normalizedLenMat / signalV
else:
Dmat = lengthMat * 0.0
return Dmat
7 changes: 3 additions & 4 deletions neurolib/models/wc/timeIntegration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import numba

from . import loadDefaultParams as dp
from ...utils import model_utils as mu


Expand Down Expand Up @@ -59,7 +58,7 @@ def timeIntegration(params):
Dmat = np.zeros((N, N))
else:
# Interareal connection delays, Dmat(i,j) Connnection from jth node to ith (ms)
Dmat = dp.computeDelayMatrix(lengthMat, signalV)
Dmat = mu.computeDelayMatrix(lengthMat, signalV)
Dmat[np.eye(len(Dmat)) == 1] = np.zeros(len(Dmat))
Dmat_ndt = np.around(Dmat / dt).astype(int) # delay matrix in multiples of dt
params["Dmat_ndt"] = Dmat_ndt
Expand Down Expand Up @@ -219,7 +218,7 @@ def S_I(x):
c_excexc * excs[no, i - 1] # input from within the excitatory population
- c_inhexc * inhs[no, i - 1] # input from the inhibitory population
+ exc_input_d[no] # input from other nodes
+ exc_ext[no, i-1]
+ exc_ext[no, i - 1]
) # external input
+ exc_ou[no] # ou noise
)
Expand All @@ -233,7 +232,7 @@ def S_I(x):
* S_I(
c_excinh * excs[no, i - 1] # input from the excitatory population
- c_inhinh * inhs[no, i - 1] # input from within the inhibitory population
+ inh_ext[no, i-1]
+ inh_ext[no, i - 1]
) # external input
+ inh_ou[no] # ou noise
)
Expand Down
19 changes: 0 additions & 19 deletions neurolib/models/ww/loadDefaultParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,3 @@ def loadDefaultParams(Cmat=None, Dmat=None, seed=None):
params.inh_ou = np.zeros((params.N,))

return params


def computeDelayMatrix(lengthMat, signalV, segmentLength=1):
"""Compute the delay matrix from the fiber length matrix and the signal velocity

:param lengthMat: A matrix containing the connection length in segment
:param signalV: Signal velocity in m/s
:param segmentLength: Length of a single segment in mm

:returns: A matrix of connexion delay in ms
"""

normalizedLenMat = lengthMat * segmentLength
# Interareal connection delays, Dmat(i,j) in ms
if signalV > 0:
Dmat = normalizedLenMat / signalV
else:
Dmat = lengthMat * 0.0
return Dmat
4 changes: 2 additions & 2 deletions neurolib/models/ww/timeIntegration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import numba

from . import loadDefaultParams as dp
from ...utils import model_utils as mu


def timeIntegration(params):
Expand Down Expand Up @@ -63,7 +63,7 @@ def timeIntegration(params):
Dmat = np.zeros((N, N))
else:
# Interareal connection delays, Dmat(i,j) Connnection from jth node to ith (ms)
Dmat = dp.computeDelayMatrix(lengthMat, signalV)
Dmat = mu.computeDelayMatrix(lengthMat, signalV)
# no self-feedback delay
Dmat[np.eye(len(Dmat)) == 1] = np.zeros(len(Dmat))
Dmat_ndt = np.around(Dmat / dt).astype(int) # delay matrix in multiples of dt
Expand Down
21 changes: 21 additions & 0 deletions neurolib/utils/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,24 @@ def adjustArrayShape(original, target):
original = original[: target.shape[0], -target.shape[1] :]

return original


def computeDelayMatrix(lengthMat, signalV, segmentLength=1):
"""
Compute the delay matrix from the fiber length matrix and the signal
velocity

:param lengthMat: A matrix containing the connection length in
segment
:param signalV: Signal velocity in m/s
:param segmentLength: Length of a single segment in mm

:returns: A matrix of connexion delay in ms
"""

normalizedLenMat = lengthMat * segmentLength
if signalV > 0:
Dmat = normalizedLenMat / signalV # Interareal delays in ms
else:
Dmat = lengthMat * 0.0
return Dmat