Skip to content

Commit

Permalink
Added option to add <Include> to generated model
Browse files Browse the repository at this point in the history
  • Loading branch information
pgleeson committed Jun 7, 2016
1 parent 9fce346 commit ea00df5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
50 changes: 50 additions & 0 deletions examples/apitest3.py
@@ -0,0 +1,50 @@
#! /usr/bin/python

import lems.api as lems

model = lems.Model()

model.add(lems.Include("test.xml"))

model.add(lems.Dimension('voltage', m=1, l=3, t=-3, i=-1))
model.add(lems.Dimension('time', t=1))
model.add(lems.Dimension('capacitance', m=-1, l=-2, t=4, i=2))

model.add(lems.Unit('milliVolt', 'mV', 'voltage', -3))
model.add(lems.Unit('milliSecond', 'ms', 'time', -3))
model.add(lems.Unit('microFarad', 'uF', 'capacitance', -12))

iaf1 = lems.ComponentType('iaf1')
model.add(iaf1)

iaf1.add(lems.Parameter('threshold', 'voltage'))
iaf1.add(lems.Parameter('reset', 'voltage'))
iaf1.add(lems.Parameter('refractoryPeriod', 'time'))
iaf1.add(lems.Parameter('capacitance', 'capacitance'))
iaf1.add(lems.Exposure('vexp', 'voltage'))
dp = lems.DerivedParameter('range', 'threshold - reset', 'voltage')
iaf1.add(dp)

iaf1.dynamics.add(lems.StateVariable('v','voltage', 'vexp'))
iaf1.dynamics.add(lems.DerivedVariable('v2',dimension='voltage', value='v*2'))
cdv = lems.ConditionalDerivedVariable('v_abs','voltage')
cdv.add(lems.Case('v .geq. 0','v'))
cdv.add(lems.Case('v .lt. 0','-1*v'))
iaf1.dynamics.add(cdv)


model.add(lems.Component('celltype_a', iaf1.name))
model.add(lems.Component('celltype_b', iaf1.name, threshold="20mV"))

fn = '/tmp/model.xml'
model.export_to_file(fn)

print("----------------------------------------------")
print(open(fn,'r').read())
print("----------------------------------------------")

print("Written generated LEMS to %s"%fn)

from lems.base.util import validate_lems

validate_lems(fn)
26 changes: 26 additions & 0 deletions lems/model/fundamental.py
Expand Up @@ -8,6 +8,32 @@

from lems.base.base import LEMSBase

class Include(LEMSBase):
"""
Include another LEMS file.
"""

def __init__(self, filename):
"""
Constructor.
@param filename: Name of the file.
@type name: str
"""

self.file = filename
""" Name of the file.
@type: str """


def toxml(self):
"""
Exports this object into a LEMS XML object
"""

return '<Include file="%s"/>'%self.file

class Dimension(LEMSBase):
"""
Stores a dimension in terms of the seven fundamental SI units.
Expand Down
23 changes: 21 additions & 2 deletions lems/model/model.py
Expand Up @@ -16,7 +16,7 @@
from lems.base.errors import ModelError
from lems.base.errors import SimBuildError

from lems.model.fundamental import Dimension,Unit
from lems.model.fundamental import Dimension,Unit,Include
from lems.model.component import Constant,ComponentType,Component,FatComponent
from lems.model.simulation import Run,Record,EventRecord,DataDisplay,DataWriter,EventWriter
from lems.model.structure import With,EventConnection,ChildInstance,MultiInstantiate
Expand Down Expand Up @@ -47,6 +47,10 @@ def __init__(self, include_includes='True'):
""" List of targets to be run on startup.
@type: list(str) """

self.includes = Map()
""" Dictionary of includes defined in the model.
@type: dict(str -> lems.model.fundamental.Include """

self.dimensions = Map()
""" Dictionary of dimensions defined in the model.
@type: dict(str -> lems.model.fundamental.Dimension """
Expand Down Expand Up @@ -98,6 +102,16 @@ def add_target(self, target):

self.targets.append(target)

def add_include(self, include):
"""
Adds an include to the model.
@param include: Include to be added.
@type include: lems.model.fundamental.Include
"""

self.includes[include.file] = include

def add_dimension(self, dimension):
"""
Adds a dimension to the model.
Expand Down Expand Up @@ -171,7 +185,9 @@ def add(self, child):
@param child: Child object to be added.
"""

if isinstance(child, Dimension):
if isinstance(child, Include):
self.add_include(child)
elif isinstance(child, Dimension):
self.add_dimension(child)
elif isinstance(child, Unit):
self.add_unit(child)
Expand Down Expand Up @@ -262,6 +278,9 @@ def export_to_file(self, filepath, level_prefix = ' '):

xmlstr = '<Lems %s>'%namespaces

for include in self.includes:
xmlstr += include.toxml()

for target in self.targets:
xmlstr += '<Target component="{0}"/>'.format(target)

Expand Down

0 comments on commit ea00df5

Please sign in to comment.