Skip to content

Commit

Permalink
Handle setting multiple node values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagirhussan committed Jun 22, 2024
1 parent 54a6194 commit f924d33
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ftuutils/codegenerationutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def exportAsODEStepper(composer,modelName):
ik = '_'.join(k.split('_')[:-1])
inputhook[ik] = v
inputhookcode = f'class {modelName}Hooks():\n #FTU parameters\n'

inputhookcode += f" CELL_COUNT = {len(composer.inodeIndexes)}\n"
inputhookcode += " ftuparameterhooks={"
if len(ftuidmap)>0:
for k,v in ftuidmap.items():
Expand Down
42 changes: 40 additions & 2 deletions src/ftuutils/simulationutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ def visit_Name(self, node):
return ast.Name(**{**node.__dict__, 'id':self.variableNameMap[node.id]})
else:
return node

def handleStars(code, numnodes):
"""Replace code that has node[*]. with node[1..numnodes].
Args:
code (string): Python code
numnodes (int): Number of nodes
"""
cstrip = code.split('\n')
ncstrip = []
for cs in cstrip:
if 'node[*]' in cs:
for ni in range(1,numnodes+1):
ncstrip.append(cs.replace('node[*]',f'node[{ni}]'))
else:
ncstrip.append(cs)
return '\n'.join(ncstrip)


def loadTemplateFile(filename):
"""Load template files stored as resources within ftuutils package"""
Expand Down Expand Up @@ -95,7 +113,7 @@ def __init__(self,composer) -> None:

code = f"{self.hamletcode}\nself.hamletNodes={self.modelname}Inputs.nodes\n"
exec(compile(code,'','exec'))

self.CELL_COUNT = self.inputhookInstance.CELL_COUNT #Number of nodes
self.variablemap = self.inputhookInstance.inputhooks
for k,v in self.inputhookInstance.statehooks.items():
for sn,sm in v.items():
Expand All @@ -107,10 +125,15 @@ def __init__(self,composer) -> None:
for k,v in self.inputhookInstance.phsparameterhooks.items():
for sn,sm in v.items():
self.inputhookInstance.inputhooks[f"node[{k}].{sn}"] = sm
#Empty ftuparameterhooks def is returned as tuple ({},)
self.ftuparameterhooks = self.inputhookInstance.ftuparameterhooks
if isinstance(self.inputhookInstance.ftuparameterhooks,tuple):
self.ftuparameterhooks = self.inputhookInstance.ftuparameterhooks[0]

for k,v in self.ftuparameterhooks.items():
self.inputhookInstance.inputhooks[k] = v


def addExperiment(self,name,time,inputblock,parameterblock=None,preamble=""):
"""Add an experiment for simulation
Expand Down Expand Up @@ -166,7 +189,8 @@ def generate(self,targetDir,provenance={},defaultnetworkid=1):
numsteps = stepsize

#Process the input code block to
eventCodex = ast.unparse(refactor.visit(ast.parse(v['process_time_sensitive_events']))).strip()
evcode = handleStars(v['process_time_sensitive_events'],self.CELL_COUNT)
eventCodex = ast.unparse(refactor.visit(ast.parse(evcode))).strip()
cx = eventCodex.split('\n')
#Indentation should match that of 'process_time_sensitive_events' function def
indent = " "
Expand All @@ -178,6 +202,20 @@ def generate(self,targetDir,provenance={},defaultnetworkid=1):
eventCode += f"{indent}{c}\n"

parameterupdates = ''
pblock = v['parameters']
if not pblock is None:
pvcode = handleStars(pblock,self.CELL_COUNT)
pCodex = ast.unparse(refactor.visit(ast.parse(pvcode))).strip()
pcx = pCodex.split('\n')
#Indentation should match that of '__init__' function def
indent = " "
if pCodex.startswith("def"):
pcx = pcx[1:]
indent = " "
parameterupdates = ""
for pc in pcx:
parameterupdates += f"{indent}{pc}\n"

code = v['preamble']
if len(code)>0:
code +='\n'
Expand Down
6 changes: 4 additions & 2 deletions tests/ftuworkflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,12 @@
"The above refers to state value `Ta`, belonging to node[1], here `1` is the label assigned to the node in the graph generation process.\n",
"\n",
"Similarly if the composite PHS is composed such that the individual phs parameters are not substituted at composition time, those parameters are also made available using the above syntax, for instance\n",
"`node[3].eps0`\n",
"`node[3].eps0`.\n",
"\n",
"Where there are more than one celltypes, say `APN`, `FHN`...\n",
"The states names and phs parameter names are prefixed by the celltype, as `APN_Ta,APN_u`, `FHN_u, FHN_v`\n",
"The states names and phs parameter names are prefixed by the celltype, as `APN_Ta,APN_u`, `FHN_u, FHN_v`.\n",
"\n",
"To apply a variation to all nodes, the special operator `*` can be used. For instance, `node[*].Ta = 5.0` will set the state value `Ta` for all nodes to `5.0`.\n",
"\n",
"Below we create a single experiment, where the boundary cells are stimulated with a current of `0.5` units between `100<t<110` time units."
]
Expand Down

0 comments on commit f924d33

Please sign in to comment.