-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simpler rate expressions + some reorganization
- Loading branch information
Showing
4 changed files
with
68 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,16 @@ | ||
from obj_model import core | ||
|
||
import sympy | ||
from six import integer_types, string_types, with_metaclass | ||
#from sympy import S | ||
#from sympy import core.all_classes as sympy_all_classes | ||
|
||
class SymbolicExpressionAttribute(core.Attribute): | ||
""" Symbolic Expression to be used in RateLaws """ | ||
def __init__(self,none=True,default=None,verbose_name='',help='Enter a Sympify-able Expression',primary=False,unique=False): | ||
if default is not None: | ||
try: | ||
default = sympy.S(default) | ||
except: | ||
raise ValueError('SymbolicExpressionAttribute must be either None or Sympify-able Expression') | ||
super(SymbolicExpressionAttribute, self).__init__(default=default,verbose_name=verbose_name,help=help,primary=primary,unique=unique) | ||
self.none = none | ||
def clean(self,value): | ||
if value is None: | ||
return (value,None) | ||
if value is '': | ||
return (value,None) | ||
if isinstance(value, tuple(sympy.core.all_classes)): | ||
return (value,None) | ||
if isinstance(value, string_types+integer_types+(float,)): | ||
return (sympy.S(value),None) | ||
|
||
class SymbolAttribute(core.Attribute): | ||
def __init__(self,none=True,default=None,verbose_name='',help='Enter a Sympy Symbol',primary=True,unique=True): | ||
if default is not None: | ||
try: | ||
default = sympy.Symbol(default) | ||
except: | ||
raise ValueError('SymbolAttribute must be a Sympy Symbol') | ||
super(SymbolAttribute, self).__init__(default=default,verbose_name=verbose_name,help=help,primary=primary,unique=unique) | ||
self.none = none | ||
def clean(self,value): | ||
if value is None: | ||
return (value,None) | ||
if value is '': | ||
return (value,None) | ||
if isinstance(value, tuple(sympy.core.all_classes)): | ||
return (value,None) | ||
if isinstance(value, string_types): | ||
return (sympy.Symbol(value),None) | ||
|
||
class Parameter(core.Model): | ||
symbol = SymbolAttribute(primary=True) | ||
symbol = core.StringAttribute(primary=True) | ||
value = core.FloatAttribute() | ||
|
||
class SymbolicExpression(core.Model): | ||
class RateExpression(core.Model): | ||
id = core.StringAttribute(primary=True) | ||
expr = SymbolicExpressionAttribute() | ||
params = core.ManyToManyAttribute(Parameter) | ||
|
||
expr = core.StringAttribute() | ||
parameters = core.ManyToManyAttribute(Parameter,related_name='expressions') | ||
|
||
def main(): | ||
k1 = Parameter(symbol=sympy.Symbol('k1'),value=1000) | ||
print( k1, type(k1), k1.symbol, type(k1.symbol), ) | ||
|
||
|
||
return | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
###### Factory ###### | ||
class Factory(object): | ||
def build(self,obj_type,names,instances=True): | ||
types = dict() | ||
vec = [] | ||
for name in names: | ||
if name not in moltypes: | ||
types[name] = type(name,(obj_type,),{}) | ||
if instances==False: | ||
vec.append( types[name] ) | ||
if instances==True: | ||
vec.append( types[name]() ) | ||
return vec | ||
|
||
###### Error ###### | ||
class AddObjectError(Exception): | ||
def __init__(self,parentobject,currentobject,allowedobjects,methodname='add()'): | ||
msg = '\nObject of ' + self.to_str(currentobject)+ ' cannot be added to ' + self.to_str(parentobject) + ' using ' + methodname + '. ' | ||
if (len(allowedobjects)==1 or isinstance(allowedobjects,str)): | ||
msg = msg + 'Only objects of type' + str(allowedobjects) + 'are allowed.' | ||
else: | ||
msg = msg + 'Only objects of type ' + ', '.join(allowedobjects) + ' are allowed.' | ||
super(AddObjectError, self).__init__(msg) | ||
@staticmethod | ||
def to_str(obj): | ||
msg = str(type(obj)) | ||
msg = ''.join([ch for ch in msg if ch not in "<>"]) | ||
return msg |