def CascadeHandler(r): DBG = False casc = r.Arrow() stages,arrow = casc.split("*") modifiers = r.MHS() if DBG: print "Cascade Handler: stages,arrow: ", stages, arrow if DBG: print "Cascade Handler: modifiers: ", modifiers # # most of the cascades in py[cellerator] don't actually exist in cellerator # so expand to the bottom line # er = expand(r) er = map(_Handle_Reaction, er) if DBG: print "Cascade Handler: er: ", er okr = map(lambda u:u[-1], filter(lambda u:u[0], er)) notok = map(lambda u:u[-1], filter(lambda u:not(u[0]), er)) if DBG: print "okr: ", okr if DBG: print "notok: ", notok for r in notok: print "Error: unable to convert the reaction: "+str(r) return okr
def CascadeHandler(r): DBG = False casc = r.Arrow() stages, arrow = casc.split("*") modifiers = r.MHS() if DBG: print "Cascade Handler: stages,arrow: ", stages, arrow if DBG: print "Cascade Handler: modifiers: ", modifiers # # most of the cascades in py[cellerator] don't actually exist in cellerator # so expand to the bottom line # er = expand(r) er = map(_Handle_Reaction, er) if DBG: print "Cascade Handler: er: ", er okr = map(lambda u: u[-1], filter(lambda u: u[0], er)) notok = map(lambda u: u[-1], filter(lambda u: not (u[0]), er)) if DBG: print "okr: ", okr if DBG: print "notok: ", notok for r in notok: print "Error: unable to convert the reaction: " + str(r) return okr
def test_single(): assert list(expand("a.{}")) == ["a."] assert list(expand("a.{doc}")) == ["a.doc"] assert list(expand("a.{doc,txt}")) == ["a.doc", "a.txt"] assert list(expand("a.{1,2,3}")) == ["a.1", "a.2", "a.3"] assert list(expand("{good,bad} mood")) == ["good mood", "bad mood"] assert list(expand("just {2,3} drinks")) == ["just 2 drinks", "just 3 drinks"] assert list(expand("soft drink{,s}")) == ["soft drink", "soft drinks"]
def expand(): print '[+] Please input the location of dictionary' loc = raw_input().replace("\'", "").strip() while not(os.path.exists(loc)): print "[x] {0} doesn't seem to exist, try again.".format(loc) loc = raw_input() print "[-] Beginning Expansion..." e = expander.expand(loc, loc+"-EXPANDED") e.expand()
def normalise(text): list_tokens = split_tokens(text) NSWs_dict = create_NSWs_dict(list_tokens) tagged_dic = tagify(NSWs_dict) classified_dic = classify(tagged_dic, list_tokens) expanded_dic = expand(classified_dic, list_tokens) normalized_text = replace_nsw(expanded_dic, list_tokens) return normalized_text # text = open('./vidu.txt').read() # print(normalise(text))
def test_multiple(): assert list(expand("{a}{b}{c}{d}{e}{f}")) == ["abcdef"] assert list(expand("{a}.{doc,txt}")) == ["a.doc", "a.txt"] assert list(expand("{a,b}.{doc}")) == ["a.doc", "b.doc"] assert list(expand("{a,b}.{doc,txt}")) == ["a.doc", "a.txt", "b.doc", "b.txt"] assert list(expand("{0,1,2,3,4,5,6,7,8,9}"*5)) == [format(i, "05") for i in range(10**5)] assert list(expand("{a}"*100)) == ["a"*100]
def classify_input(parsed_reactions, ncells, nspecie, PRINT=True): # summarize input # classes = {} for r in parsed_reactions: reaction_class = r.arrowtype + " " + r.arrow if reaction_class in classes: classes[reaction_class] += 1 else: classes[reaction_class] = 1 expanded_reactions = expander.expand(parsed_reactions) class_names = list(classes) if PRINT: print_rule() print "%5d" % (len(parsed_reactions)), "reactions input." print "%5d" % (len(expanded_reactions)), " expanded reactions" for class_name in class_names: print "%5d" % (classes[class_name]), class_name, "reactions" print "%5d" % (ncells * nspecie), " differential equations" print_rule() return expanded_reactions
def classify_input(parsed_reactions, ncells, nspecie, PRINT=True): # summarize input # classes = {} for r in parsed_reactions: reaction_class=r.arrowtype+" "+r.arrow if reaction_class in classes: classes[reaction_class]+=1 else: classes[reaction_class]=1 expanded_reactions = expander.expand(parsed_reactions) class_names=list(classes) if PRINT: print_rule() print "%5d" %(len(parsed_reactions)), "reactions input." print "%5d" %(len(expanded_reactions)), " expanded reactions" for class_name in class_names: print "%5d" %(classes[class_name]), class_name, "reactions" print "%5d" %(ncells*nspecie), " differential equations" print_rule() return expanded_reactions
def test_stray_braces(): assert list(expand(":-{")) == [":-{"] assert list(expand("oh }{ nice")) == ["oh }{ nice"]
def test_trivial(): assert list(expand("")) == [""] assert list(expand("hello")) == ["hello"]
def genmodel(infile, outfile, verbose=False): (reactions, ic, rates, frozenvars, functions, assignments, filename) = readmodel(infile) # Expand reactions # r = interpreter.invokeParser(reactions, dump=False) er = expander.expand(r) s = interpreter.makeSymbolDictionary(er, rates, ic) try: SBML=SBMLDocument(3,1) except ValueError: exit("Could not create the SBMLDocument.") m = SBML.createModel() m.setTimeUnits("dimensionless") m.setSubstanceUnits("item") m.setExtentUnits("item") c = m.createCompartment() c.setId('compartment') c.setConstant(True) c.setSize(1) c.setSpatialDimensions(3) c.setUnits("dimensionless") for function in functions: if verbose: print "function: ", function fnew=m.createFunctionDefinition() fname, formula = function.split("=") fname=fname.strip() formula=formula.strip() formula=formula.replace("**","^") functionvar, functiondef=formula.split(":") # print "functiondef:", functiondef functionvar=functionvar.split()[-1] # print "functionvar:", functionvar lambdadef="lambda("+functionvar+","+functiondef+")" # print "lambdadef: ", lambdadef math_ast = parseL3Formula(lambdadef) # print "math_ast=",math_ast fnew.setId(fname) fnew.setMath(math_ast) species=list(ic) sp=[] for specie in species: snew=m.createSpecies() snew.setId(specie) if specie in frozenvars: snew.setConstant(True) else: snew.setConstant(False) snew.setBoundaryCondition(False) snew.setHasOnlySubstanceUnits(True) amount = ic[specie] snew.setInitialAmount(amount) snew.setCompartment("compartment") sp.append(snew) # add nil species snew=m.createSpecies() snew.setId("Nil") snew.setConstant(False) snew.setBoundaryCondition(False) snew.setHasOnlySubstanceUnits(True) snew.setInitialAmount(0) snew.setCompartment("compartment") sp.append(snew) RATES=list(rates) ks=[] for rate in rates: k = m.createParameter() k.setId(rate) k.setConstant(True) k.setValue(rates[rate]) k.setUnits('dimensionless') ks.append(k) #print assignments i=1 for assignment in assignments: lhs, rhs = assignment.split("=") lhs=lhs.strip() rhs=rhs.strip() if verbose: print "assignment: to:", lhs, "from:",rhs arule = m.createAssignmentRule() patchedlaw=rhs.replace("**","^") math_ast = parseL3Formula(patchedlaw) arule.setMath(math_ast) arule.setVariable(lhs) aid = "arule"+str(i) i+=1 arule.setId(aid) reacts=[] i = 1 for line in er: new_reaction=m.createReaction() rid = "r"+str(i) new_reaction.setId(rid) new_reaction.setReversible(False) new_reaction.setFast(False) odeterm = interpreter.makeODETerms([line], s, frozen=frozenvars) termkeys=list(odeterm) # print "termkeys:", termkeys # print "**** Reaction ", i,"\n", line.LHS(),"\n",line.MHS(),"\n",line.RHS() S, ST = reduceStoichiometry(line.LHS(), line.LH_STOIC()) # print "reactant: ", S, ST for X,Z in zip(S, ST): if X in termkeys: sref=new_reaction.createReactant() sref.setSpecies(X) sref.setStoichiometry(float(Z)) if X in frozenvars: sref.setConstant(True) else: sref.setConstant(False) # # some Cellerator "Reactants" are really modifiers # elif str(X) != "Nil": sref=new_reaction.createModifier() sref.setSpecies(X) S, ST = reduceStoichiometry(line.RHS(), line.RH_STOIC()) # print "product: ", S, ST for X,Z in zip(S, ST): sref=new_reaction.createProduct() sref.setSpecies(X) sref.setStoichiometry(float(Z)) if X in frozenvars: sref.setConstant(True) else: sref.setConstant(False) for X in line.MHS(): sref=new_reaction.createModifier() sref.setSpecies(X) terms = [str(odeterm[X]) for X in termkeys] # print "terms: ", terms terms = map(lambda x: x.lstrip("-"), terms) terms = set(terms) - set(["0"]) if len(terms)>0: law = list(terms)[0] else: law = "0" if verbose: print "using kinetic law ", law, " for "+str(rid)+": ", line.Input() patchedlaw=law.replace("**","^") math_ast = parseL3Formula(patchedlaw) kinetic_law = new_reaction.createKineticLaw() kinetic_law.setMath(math_ast) # print "kinetic law ", i, " is ", law, odeterm i +=1 return (writeSBMLToString(SBML))
def generatePythonFunction(reactions, rates, ics, frozenvars, functions, assignments, holdrates=[], substituteRates=False): """ input: reactions - list of reactions in cellerator text form inputrates - list of rate constants in dictionary form output: writes a function to file tmp.py that is solver-compatible return values: (y, y0) y: list of string names of variables ["y1", "y2",...] y0: list of values of variables at initial time """ #print "solver:assignments:",assignments #print "solver:frozenvars:",frozenvars d = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") codefile = utils.uniqueFileName("tmp.py") f = open(codefile, "w") f.write("from math import *\n") if len(functions) > 0: f.write("\n") for funct in functions: f.write(funct + "\n") f.write("\n") f.write("def ode_function_rhs(y,t):\n") f.write(" # \n") f.write(" # this odeint(..) compatible function was \n") f.write(" # automatically generated by Cellerator " + d + " \n") ver = sys.version.replace("\n", " ") f.write(" # " + ver + "\n") f.write(" # " + sys.platform + "\n") f.write(" # \n") f.write( " # =============================================================\n") f.write(" # Model: \n #\n") # # there may be a whole boatload of reactions to print # define a generator def printable_reaction(k): for j in xrange(k): reaction = reactions[j] yield " # " + reaction.strip() + "\n" for r in printable_reaction(len(reactions)): f.write(r) #for reaction in reactions: # f.write(" # " + reaction.strip() + "\n") # # write a whole shitload of comments with the rate constants, unless # the rate constants are themselves written out # if substituteRates: f.write(" #\n # Parameter values used: \n #\n") for r in rates: f.write(" # " + r + "=" + str(rates[r]) + "\n") if len(holdrates) > 0: f.write(" global " + holdrates[0] + "\n") if len(frozenvars) > 0: f.write( " #\n # Frozen Variables (species with fixed derivatives):\n #\n" ) for v in frozenvars: f.write(" # " + str(v) + "\n") f.write( " # =============================================================\n") results = interpreter.invokeParser(reactions, dump=False) results = expand(results) s = interpreter.makeSymbolDictionary(results, rates, ics) odeterms = interpreter.makeODETerms(results, s, frozen=frozenvars) if substituteRates: newterms = substituteRateConstants(odeterms, rates, s, holdrates) na = [] # rate constants also appear in assignment rules for a in assignments: value, assignment = a.split("=") assignment = sympify(assignment) newassignment = value + "=" + str( applyReplacementRules(assignment, rates, s)) na.append(newassignment) assignments = na else: # better this way because applyReplacementRules grows superquadratically newterms = odeterms ks = rates.keys() ks.sort() f.write(" # rate constants\n") for k in ks: if k in holdrates: continue v = rates[k] nextline = " " + k + " = " + str(v) + "\n" f.write(nextline) (y, yprime, ics) = makeODEfunc(newterms, s, ics) y = map(str, y) yprime = map(str, yprime) f.write("# pick up values from previous iteration\n") n = len(y) for i in range(n): nextline = " " + y[i] + " = " + "max(0, y[" + str(i) + "])\n" # nextline = nextline.replace("{", "[").replace("}", "]") # f.write(nextline) if (len(assignments) > 0): f.write("# apply boundary conditions / assignment rules \n") for a in assignments: f.write(" " + a + "\n") f.write("# calculate derivatives of all variables\n") f.write(" yp=[0 for i in range(" + str(n) + ")]\n") for i in range(n): nextline = " yp[" + str(i) + "] = " + yprime[i] + "\n" # # # nextline = nextline.replace("{", "[").replace("}", "]") # # f.write(nextline) #print "solver:",nextline f.write(" return yp\n") f.close() #sys.exit() return (y, ics, codefile)
def interpret(filename, ofile="", dumpparser=False, format="ODE", frozen=[], \ symbolic=False): if "-time" in sys.argv: start = clock() DBG = False if DBG: print "interpret: Starting" output=[] dump = dumpparser or ("-dump" in sys.argv) SYMBOLIC = symbolic or "-symbolic" in sys.argv freeze=[] if ("-frozen" in sys.argv): i=(sys.argv).index("-frozen")+1 while i<len(sys.argv): next = sys.argv[i] if str(next)[0]=="-": break freeze.append(next) i+=1 elif type(frozen)==type([]): freeze = frozen if DBG: print "interpret: calling Parser" (results, rates, functions, frozen, assignments, ICS) = runparser(filename, trace=dump) # # combine frozen variables on command line with those in model file # freeze = list (set (freeze + frozen)) # print rates if DBG: print "interpret: expanding" if "-time" in sys.argv: parsetime = clock()-start print "(parse time): %20.10f" %(parsetime) start = clock() results = expand(results) if DBG: print "interpret: making Symbol Dicitionary" s=makeSymbolDictionary(results, rates, ICS) if DBG: print "interpret: making ODE Terms" odeterms = makeODETerms(results, s, frozen=freeze) if DBG: print "interpret: printing terms" # print odeterms if "-format" in sys.argv: i = (sys.argv).index("-format")+1 if i < len(sys.argv): format = sys.argv[i] else: print "Error: interpret: expecting format string after -format keyword." format = "ODE" fmt = str(format).upper() if not fmt in ["ODE","DICT","CODE", "PYTHON", "LATEX", "JACOBIAN", "STEADYSTATE"]: print "Error: interpret: invalid format='"+str(fmt)+"' requested." raise SystemExit("Resubmit job with correct value for keyword -format") k=0 if fmt in ["CODE","PYTHON"]: output.append("from math import *") if len(functions)>0: for f in functions: output.append(f) output.append("def f(y,t):") for x in rates: output.append(" "+str(x)+" = "+str(rates[x])) for x in assignments: output.append(" "+str(x)) for x in odeterms: line = " y["+str(k)+"] = " + str(x) output.append(line) k += 1 k = 0 elif fmt == "DICT": output=odeterms return (output) # if symbolic: return output elif fmt == "JACOBIAN": output = jacobian(odeterms) return (output) # if symbolic: return output elif fmt == "STEADYSTATE": output = steadystate(odeterms, rates, SYMBOLIC) elif fmt == "LATEX": output.append("\\documentclass[12pt,letterpaper]{article}") output.append("\\usepackage[latin1]{inputenc}") output.append("\\usepackage{amsmath, amsfonts, amssymb}") output.append("\\author{py[cellerator]}") output.append("\\date{\\today}") output.append("\\title{Automatically Generated Equations}") output.append("\\begin{document}") output.append("\\maketitle") output.append("\\begin{align*}") for f in functions: output.append(latex(f)) if fmt == "ODE": for f in functions: output.append(f) for f in assignments: output.append(f) for x in odeterms: if fmt == "ODE": line = str(x)+"' = "+str(odeterms[x]) elif fmt == "DICT": continue elif fmt == "JACOBIAN": continue elif fmt == "STEADYSTATE": continue elif fmt in ["CODE", "PYTHON"]: line = " yp["+str(k)+"] = " + str(odeterms[x]) k+=1 elif fmt == "LATEX": line =x+"' &= "+latex(odeterms[x])[1:-1]+"\\\\" else: print "Error: interpret: invalid format requested: " + str(format) raise SystemExit("Resubmit job with correct value for keyword.") output.append(line) if fmt =="ODE": output = "\n".join(output) elif fmt == "LATEX": output.append("\\end{align*}") output.append("\\end{document}") output = "\n".join(output) elif fmt in ["CODE", "PYTHON"]: output.append(" return (yp)") output = "\n".join(output) outfile=ofile if ("-out" in sys.argv): i = (sys.argv).index("-out")+1 if i<len(sys.argv): outfile=sys.argv[i] else: print("Error: interpret: expecting filename after -out option.") outfile="interpreted-equations.out" if len(outfile)>0: outputfile = outfile (left,right)=outfile.split(".") j=1 while os.path.isfile(outputfile): outputfile=left+str(j)+"."+right j+=1 f=open(outputfile,"w") if fmt == "DICT": output = str(output) f.write(output) else: f.write(output) # output = map(lambda x:x+"\n",output) # f.writelines(output) f.close() if "-time" in sys.argv: print "(interpret time): %20.10f" %(clock() - start) return os.path.abspath(outputfile) if "-time" in sys.argv: print "(interpret time): %20.10f" %(clock() - start) return(output)
def generatePythonFunction(reactions, rates, ics, frozenvars, functions, assignments, holdrates=[], substituteRates=False): """ input: reactions - list of reactions in cellerator text form inputrates - list of rate constants in dictionary form output: writes a function to file tmp.py that is solver-compatible return values: (y, y0) y: list of string names of variables ["y1", "y2",...] y0: list of values of variables at initial time """ #print "solver:assignments:",assignments #print "solver:frozenvars:",frozenvars d=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") codefile=utils.uniqueFileName("tmp.py") f=open(codefile,"w") f.write("from math import *\n") if len(functions)>0: f.write("\n") for funct in functions: f.write(funct+"\n") f.write("\n") f.write("def ode_function_rhs(y,t):\n") f.write(" # \n") f.write(" # this odeint(..) compatible function was \n") f.write(" # automatically generated by Cellerator "+d+" \n") ver = sys.version.replace("\n"," ") f.write(" # " + ver + "\n") f.write(" # " + sys.platform + "\n") f.write(" # \n") f.write(" # =============================================================\n") f.write(" # Model: \n #\n") # # there may be a whole boatload of reactions to print # define a generator def printable_reaction(k): for j in xrange(k): reaction=reactions[j] yield " # " + reaction.strip() + "\n" for r in printable_reaction(len(reactions)): f.write(r) #for reaction in reactions: # f.write(" # " + reaction.strip() + "\n") # # write a whole shitload of comments with the rate constants, unless # the rate constants are themselves written out # if substituteRates: f.write(" #\n # Parameter values used: \n #\n") for r in rates: f.write(" # " + r + "=" + str(rates[r]) + "\n") if len(holdrates)>0: f.write(" global "+holdrates[0]+"\n") if len(frozenvars) >0: f.write(" #\n # Frozen Variables (species with fixed derivatives):\n #\n") for v in frozenvars: f.write(" # "+str(v)+ "\n") f.write(" # =============================================================\n") results = interpreter.invokeParser(reactions, dump=False) results = expand(results) s=interpreter.makeSymbolDictionary(results, rates, ics) odeterms = interpreter.makeODETerms(results, s, frozen=frozenvars) if substituteRates: newterms = substituteRateConstants(odeterms, rates, s, holdrates) na = [] # rate constants also appear in assignment rules for a in assignments: value,assignment = a.split("=") assignment = sympify(assignment) newassignment = value+"="+str(applyReplacementRules(assignment, rates, s)) na.append(newassignment) assignments = na else: # better this way because applyReplacementRules grows superquadratically newterms = odeterms ks=rates.keys() ks.sort() f.write(" # rate constants\n") for k in ks: if k in holdrates: continue v=rates[k] nextline=" "+k+" = " + str(v)+"\n" f.write(nextline) (y, yprime, ics) = makeODEfunc(newterms, s, ics) y = map(str, y) yprime = map(str,yprime) f.write("# pick up values from previous iteration\n") n = len(y) for i in range(n): nextline = " "+y[i]+" = "+"max(0, y["+str(i)+"])\n" # nextline = nextline.replace("{","[").replace("}","]") # f.write(nextline) if (len(assignments)>0): f.write("# apply boundary conditions / assignment rules \n") for a in assignments: f.write(" "+a+"\n") f.write("# calculate derivatives of all variables\n") f.write(" yp=[0 for i in range("+str(n)+")]\n") for i in range(n): nextline = " yp["+str(i)+"] = " + yprime[i]+"\n" # # # nextline = nextline.replace("{","[").replace("}","]") # # f.write(nextline) #print "solver:",nextline f.write(" return yp\n") f.close() #sys.exit() return (y, ics, codefile)