Skip to content

Commit

Permalink
Merge pull request #565 from sys-bio/develop
Browse files Browse the repository at this point in the history
Push changes to release.
  • Loading branch information
luciansmith authored Jan 9, 2023
2 parents 43163a0 + c1f4911 commit b8465b2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tellurium/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.4
2.2.5
8 changes: 4 additions & 4 deletions tellurium/roadrunner/extended_roadrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def exportToSBML(self, filePath, current=True):
:param filePath: file path of SBML file
:type filePath: str
"""
with open(filePath, 'w') as f:
with open(filePath, 'w', encoding="utf-8") as f:
f.write(self.__getSBML(current))

def exportToAntimony(self, filePath, current=True):
Expand All @@ -193,7 +193,7 @@ def exportToAntimony(self, filePath, current=True):
:param filePath: file path of Antimony file
:type filePath: str
"""
with open(filePath, 'w') as f:
with open(filePath, 'w', encoding="utf-8") as f:
f.write(self.getAntimony(current))

def exportToCellML(self, filePath, current=True):
Expand All @@ -204,7 +204,7 @@ def exportToCellML(self, filePath, current=True):
:param filePath: file path of CellML file
:type filePath: str
"""
with open(filePath, 'w') as f:
with open(filePath, 'w', encoding="utf-8") as f:
f.write(self.getCellML(current))

def exportToMatlab(self, filePath, current=True):
Expand All @@ -217,7 +217,7 @@ def exportToMatlab(self, filePath, current=True):
:param filePath: file path of Matlab file
:type filePath: str
"""
with open(filePath, 'w') as f:
with open(filePath, 'w', encoding="utf-8") as f:
f.write(self.getMatlab(current))

# ---------------------------------------------------------------------
Expand Down
29 changes: 18 additions & 11 deletions tellurium/sedml/mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,36 @@ def f_or(*args):
return 0
"""

def renameNPFuncs(astnode):
if astnode.isFunction():
name = astnode.getName()
if name=="max":
astnode.setName("np.nanmax")
if name=="min":
astnode.setName("np.nanmin")
if name=="sum":
astnode.setName("np.nansum")
if name=="product":
astnode.setName("np.prod")
for c in range(astnode.getNumChildren()):
renameNPFuncs(astnode.getChild(c))

def evaluableMathML(astnode, variables={}, array=False):
""" Create evaluable python string.
"""
# replace variables with provided values
for key, value in variables.items():
astnode.replaceArgument(key, libsbml.parseFormula(str(value)))

if array:
renameNPFuncs(astnode)
# get formula
formula = libsbml.formulaToL3String(astnode)

# <replacements>
# FIXME: these are not exhaustive, but are improved with examples
if array is False:
# scalar
formula = formula.replace("&&", 'and')
formula = formula.replace("||", 'or')
else:
# np.array
formula = formula.replace("max", 'np.nanmax')
formula = formula.replace("min", 'np.nanmin')
formula = formula.replace("sum", 'np.nansum')
formula = formula.replace("product", 'np.prod')
formula = formula.replace("&&", 'and')
formula = formula.replace("||", 'or')

return formula

Expand Down
82 changes: 78 additions & 4 deletions tellurium/sedml/tesedml.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ def getKisaoStringFromVal(val):
413,
432,
437,
568,
569,
]

# allowed algorithms for simulation type
Expand Down Expand Up @@ -327,7 +329,13 @@ def sedmlToPython(inputStr, workingDir=None):
return factory.toPython()


def executeSEDML(inputStr, workingDir=None):
def executeSEDML(inputStr,
workingDir=None,
createOutputs=True,
saveOutputs=False,
outputDir=None,
plottingEngine=None
):
""" Run a SED-ML file or combine archive with results.
If a workingDir is provided the files and results are written in the workingDir.
Expand All @@ -338,7 +346,12 @@ def executeSEDML(inputStr, workingDir=None):
:rtype:
"""
# execute the sedml
factory = SEDMLCodeFactory(inputStr, workingDir=workingDir)
factory = SEDMLCodeFactory(inputStr, workingDir=workingDir,
createOutputs=createOutputs,
saveOutputs=saveOutputs,
outputDir=outputDir,
plottingEngine=plottingEngine
)
factory.executePython()


Expand Down Expand Up @@ -590,6 +603,16 @@ def executePython(self):
with open(filename, 'w') as f:
f.write(code)
raise
def localParamsInModelChanges(self):
"""
Returns True if any of the model changes target local parameters.
"""
for mod in self.model_changes:
for change in self.model_changes[mod]:
target = change.getTarget()
if "kineticLaw" in target and "arameter" in target:
return True
return False

def modelToPython(self, model):
""" Python code for SedModel.
Expand Down Expand Up @@ -636,6 +659,13 @@ def isHttp():
else:
warnings.warn("Unsupported model language: '{}'.".format(language))

# If there are local parameters pointed to by the model changes, convert
# the model first
if self.localParamsInModelChanges():
lines.append("# Promote the local parameters, since there are model changes that target them, and roadrunner doesn't normally provide access.")
lines.append("promoted = {}.getParamPromotedSBML({}.getCurrentSBML())".format(mid, mid))
lines.append("{} = te.loadSBMLModel(promoted)".format(mid))

# apply model changes
for change in self.model_changes[mid]:
lines.extend(SEDMLCodeFactory.modelChangeToPython(model, change))
Expand Down Expand Up @@ -1598,6 +1628,18 @@ def getId(xpath):
warnings.warn("Xpath could not be resolved: {}".format(xpath))
return match[0]

def getAllIds(xpath):
xpath = xpath.replace('"', "'")
match = re.findall(r"id='(.*?)'", xpath)
if (match is None) or (len(match) == 0):
warnings.warn("Xpath could not be resolved: {}".format(xpath))
return match

#Local parameter value change
if ("model" in xpath) and ("parameter" in xpath) and ("reaction" in xpath):
(rxn, param) = getAllIds(xpath)
return Target(rxn + "_" + param, 'parameter')

# parameter value change
if ("model" in xpath) and ("parameter" in xpath):
return Target(getId(xpath), 'parameter')
Expand Down Expand Up @@ -1823,6 +1865,32 @@ def outputPlot2DToPython(self, doc, output):
xtitle = ''
if oneXLabel:
xtitle = allXLabel

#X axis
xmin = None
xmax = None
if output.isSetXAxis():
xaxis = output.getXAxis()
if xaxis.isSetName():
xtitle = xaxis.getName()
if xaxis.isSetMin():
xmin = xaxis.getMin()
if xaxis.isSetMax():
xmax = xaxis.getMax()

#y ayis
ymin = None
ymax = None
ytitle = ""
if output.isSetYAxis():
yaxis = output.getYAxis()
if yaxis.isSetName():
ytitle = yaxis.getName()
if yaxis.isSetMin():
ymin = yaxis.getMin()
if yaxis.isSetMax():
ymax = yaxis.getMax()


lines.append("_stacked = False")
# stacking, currently disabled
Expand All @@ -1833,9 +1901,9 @@ def outputPlot2DToPython(self, doc, output):
# lines.append("if {}.shape[1] > 1 and te.getDefaultPlottingEngine() == 'plotly':".format(xId))
# lines.append(" stacked=True")
lines.append("if _stacked:")
lines.append(" tefig = te.getPlottingEngine().newStackedFigure(title='{}', xtitle='{}')".format(title, xtitle))
lines.append(" tefig = te.getPlottingEngine().newStackedFigure(title='{}', xtitle='{}', ytitle='{}', xlim=({}, {}), ylim=({}, {}))".format(title, xtitle, ytitle, xmin, xmax, ymin, ymax))
lines.append("else:")
lines.append(" tefig = te.nextFigure(title='{}', xtitle='{}')\n".format(title, xtitle))
lines.append(" tefig = te.nextFigure(title='{}', xtitle='{}', ytitle='{}', xlim=({}, {}), ylim=({}, {}))\n".format(title, xtitle, ytitle, xmin, xmax, ymin, ymax))

lastvbar = []
lasthbar = []
Expand Down Expand Up @@ -1883,6 +1951,8 @@ def outputPlot2DToPython(self, doc, output):
(showLine, ltype, dashes) = line_types[line.getType()]
if line.isSetColor():
color = line.getColor()
if color[0] != "#":
color = "#" + color
if line.isSetThickness():
lthickness = line.getThickness()
if style.isSetMarkerStyle():
Expand All @@ -1893,13 +1963,17 @@ def outputPlot2DToPython(self, doc, output):
mfc = marker.getFill()
if marker.isSetLineColor():
mec = marker.getLineColor()
if mec[0] != "#":
mec = "#" + mec
if marker.isSetSize():
ms = marker.getSize()
if marker.isSetLineThickness():
mew = marker.getLineThickness()
if style.isSetFillStyle():
fill = style.getFillStyle()
fillcolor = fill.getColor()
if fillcolor[0] != "#":
fillcolor = "#" + fillcolor

tag = 'tag{}'.format(kc)

Expand Down

0 comments on commit b8465b2

Please sign in to comment.