Skip to content

Commit

Permalink
moved computedelaymatrix function to /utils/model_utils (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenasal committed May 2, 2023
1 parent e37fb9a commit 6771599
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 122 deletions.
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
3 changes: 1 addition & 2 deletions neurolib/models/fhn/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 @@ -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
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
3 changes: 1 addition & 2 deletions neurolib/models/hopf/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
# ------------------------------------------------------------------------
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
3 changes: 1 addition & 2 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
# ------------------------------------------------------------------------
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

0 comments on commit 6771599

Please sign in to comment.