Skip to content

Commit

Permalink
Merge pull request #126 from neurolib-dev/improvement/minimal_model_i…
Browse files Browse the repository at this point in the history
…mplementation

Minimal custom model implementation
  • Loading branch information
caglorithm authored Jan 20, 2021
2 parents 4382081 + 3d7fab3 commit c4d9512
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Example [IPython Notebooks](examples/) on how to use the library can be found in

- [Example 0.0](https://mybinder.org/v2/gh/neurolib-dev/neurolib/master?filepath=examples%2Fexample-0-aln-minimal.ipynb) - Basic use of the `aln` model
- [Example 0.3](https://mybinder.org/v2/gh/neurolib-dev/neurolib/master?filepath=examples%2Fexample-0.3-fhn-minimal.ipynb) - Fitz-Hugh Nagumo model `fhn` on a brain network
- [Example 0.6](https://mybinder.org/v2/gh/neurolib-dev/neurolib/master?filepath=examples%2Fexample-0.6-custom-model.ipynb) - Minimal example of how to implement your own model in `neurolib`
- [Example 1.2](https://mybinder.org/v2/gh/neurolib-dev/neurolib/master?filepath=examples%2Fexample-1.2-brain-network-exploration.ipynb) - Parameter exploration of a brain network and fitting to BOLD data
- [Example 2.0](https://mybinder.org/v2/gh/neurolib-dev/neurolib/master?filepath=examples%2Fexample-2-evolutionary-optimization-minimal.ipynb) - A simple example of the evolutionary optimization framework

Expand Down
254 changes: 254 additions & 0 deletions examples/example-0.6-custom-model.ipynb

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions neurolib/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def __init__(self, integration, params):
if hasattr(self, "name"):
if self.name is not None:
assert isinstance(self.name, str), f"Model name is not a string."
else:
self.name = "Noname"

assert integration is not None, "Model integration function not given."
self.integration = integration
Expand All @@ -33,6 +35,10 @@ def __init__(self, integration, params):

assert isinstance(self.default_output, str), "`default_output` must be a string."

# if no output_vars is set, it will be replaced by state_vars
if not hasattr(self, "output_vars"):
self.output_vars = self.state_vars

# create output and state dictionary
self.outputs = dotdict({})
self.state = dotdict({})
Expand Down Expand Up @@ -301,7 +307,8 @@ def clearModelState(self):
self.state = dotdict({})
self.outputs = dotdict({})
# reinitialize bold model
self.initializeBold()
if self.params.get("bold"):
self.initializeBold()

def storeOutputsAndStates(self, t, variables, append=False):
"""Takes the simulated variables of the integration and stores it to the appropriate model output and state object.
Expand Down Expand Up @@ -397,18 +404,24 @@ def getMaxDelay(self):
:return: maxmimum delay of the model in units of dt
:rtype: int
"""
dt = self.params["dt"]
Dmat = self.params["lengthMat"]
dt = self.params.get("dt")
Dmat = self.params.get("lengthMat")

if "signalV" in self.params:
signalV = self.params["signalV"]
if Dmat is not None:
# divide Dmat by signalV
signalV = self.params.get("signalV") or 0
if signalV > 0:
Dmat = Dmat / signalV
else:
# if signalV is 0, eliminate delays
Dmat = Dmat * 0.0

Dmat_ndt = np.around(Dmat / dt) # delay matrix in multiples of dt
max_global_delay = int(np.amax(Dmat_ndt))
# only if Dmat and dt exist, a global max delay can be computed
if Dmat is not None and dt is not None:
Dmat_ndt = np.around(Dmat / dt) # delay matrix in multiples of dt
max_global_delay = int(np.amax(Dmat_ndt))
else:
max_global_delay = 0
return max_global_delay

def setStateVariables(self, name, data):
Expand Down

0 comments on commit c4d9512

Please sign in to comment.