旧Python代码的更新

时间:2017-11-20 12:39:47

标签: python numpy matplotlib scipy odeint

漂亮的noob问题所以请耐心等待我,因为我是Python的新手。下面给出的代码是关于生成ODE的自动组合的常微分方程。我试图在Python 3.6.3和Spider(Pyton 3.6)上执行此代码,但没有结果。我花了很多天才使它在新版本的Python上可执行,但我无法执行它,因为产生了更多的错误。所以我按原样复制了代码。

from scipy import* 
from scipy.integrate import odeint
from operator import itemgetter
import matplotlib
matplotlib.use('Agg')
from matplotlib.ticker import FormatStrFormatter
from pylab import *
from itertools import product, islice
from numpy import zeros_like
from string import ascii_lowercase
import operator

import sys


t_range = arange(0.0, 20.0, 0.1)
# initial_condi = []
# VarList = []
# ParamsList = []
ops = "+-"
opsdict = { "+": operator.add, "-": operator.sub }

# if len(initial_condi)!=len(VarList):
    # sys.exit('error: the number of initional conditions do not equal to the number of variables')


def odeFunc(Y, t, model, K):
    return GenModel(Y, model, K)


def GenModel(Y, model, K):
    # New array of floating-point zeros the size of Y
    dydt = zeros_like(Y)
    for i, derivative in enumerate(model):
        for j, operator in enumerate(derivative):
            # Sequentially compute dy/dt for each variable in Y
            dydt[i] = opsdict[operator](dydt[i],K[j]*Y[j])
    return dydt

# Returns a nicer-looking string for a derivative expression given the encoding
def derivString(vars, deriv):
    result = ""
    for i, v in enumerate(vars):
        if i == 0 and deriv[i] == '+':
            result += v
        else:
            result += deriv[i]+v
    return result

# main
numvars = int(raw_input("Enter number of variables\n> "))
VarList = ascii_lowercase[:numvars]
nummodels = (2**numvars)**numvars

# begin looping
input = ""
while input != "quit":
    print "\n%d possible models" % nummodels
    input = raw_input("Enter model ID (zero-indexed) or 'quit'\n> ")
    # Basic input filtering
    if input == "quit":
        continue
    elif not input.isdigit():
        print "Input must be a non-negative integer"
        continue
    ID = int(input)
    if ID >= nummodels or ID < 0:
        print "Invalid ID"
        continue

    # itertools.product creates a generator for all possible combinations of +-
    derivatives = product(ops, repeat=numvars)
    # We take the product again to generate all models
    models = product(derivatives, repeat=numvars)
    # islice takes the specified model
    model = next(islice(models, ID, None))

    # Display dy/dt for each variable
    print "Model %d:" % ID
    IDtitle = []
    for i, variable in enumerate(VarList):
        tempstring = "d%c/dt = %s" % (variable, derivString(VarList, model[i]))
        IDtitle.append(tempstring)
        print "\t" + tempstring

    # User specifies the initial values of all variables.
    # This process can be automated but this is to demonstrate that the progam
    # accepts any input
    init_cons = []
    params = []

    confirm = ""
    while confirm not in ("y", "n"):
        confirm = raw_input("Run this model? (y/n)\n> ")
    if confirm == "n":
        continue

    print "\nEnter <initial value, parameter> pairs separated by ','"
    for i, variable in enumerate(VarList):
        iv_param = map(float, raw_input("> %c: " % variable).split(','))
        init_cons.append(iv_param[0])
        params.append(iv_param[1])

    print "\nRunning ODEint...",
    result = odeint(odeFunc, init_cons, t_range, args=(model,params))
    print " done."

    print "Plotting results...",
    f = figure(ID)
    title(", ".join(IDtitle))
    for i, variable in enumerate(VarList):
        plot(t_range, result[:,i], label=variable)
    legend()
    axhline(0, color='k')
    savefig("model"+str(ID))
    close(f)
    print "done."

print " -- Bye --"

1 个答案:

答案 0 :(得分:1)

raw_input现在在Python3中被称为input。您还有一个名为input的变量,可能会引起混淆。

from scipy import * 
from scipy.integrate import odeint
from operator import itemgetter
import matplotlib
matplotlib.use('Agg')
from matplotlib.ticker import FormatStrFormatter
from pylab import *
from itertools import product, islice
from numpy import zeros_like
from string import ascii_lowercase
import operator
import sys

t_range = arange(0.0, 20.0, 0.1)
# initial_condi = []
# VarList = []
# ParamsList = []
ops = "+-"
opsdict = { "+": operator.add, "-": operator.sub }

# if len(initial_condi)!=len(VarList):
    # sys.exit('error: the number of initional conditions do not equal to the number of variables')

def odeFunc(Y, t, model, K):
    return GenModel(Y, model, K)

def GenModel(Y, model, K):
    # New array of floating-point zeros the size of Y
    dydt = zeros_like(Y)
    for i, derivative in enumerate(model):
        for j, operator in enumerate(derivative):
            # Sequentially compute dy/dt for each variable in Y
            dydt[i] = opsdict[operator](dydt[i],K[j]*Y[j])
    return dydt

# Returns a nicer-looking string for a derivative expression given the encoding
def derivString(vars, deriv):
    result = ""
    for i, v in enumerate(vars):
        if i == 0 and deriv[i] == '+':
            result += v
        else:
            result += deriv[i]+v
    return result

# main
numvars = int(input("Enter number of variables\n> "))
VarList = ascii_lowercase[:numvars]
nummodels = (2**numvars)**numvars

# begin looping
input_ = ""
while input_ != "quit":
    print("\n%d possible models" % nummodels)
    input_ = input("Enter model ID (zero-indexed) or 'quit'\n> ")
    # Basic input filtering
    if input_ == "quit":
        continue
    elif not input_.isdigit():
        print("Input must be a non-negative integer")
        continue
    ID = int(input_)
    if ID >= nummodels or ID < 0:
        print("Invalid ID")
        continue

    # itertools.product creates a generator for all possible combinations of +-
    derivatives = product(ops, repeat=numvars)
    # We take the product again to generate all models
    models = product(derivatives, repeat=numvars)
    # islice takes the specified model
    model = next(islice(models, ID, None))

    # Display dy/dt for each variable
    print("Model %d:" % ID)
    IDtitle = []
    for i, variable in enumerate(VarList):
        tempstring = "d%c/dt = %s" % (variable, derivString(VarList, model[i]))
        IDtitle.append(tempstring)
        print("\t" + tempstring)

    # User specifies the initial values of all variables.
    # This process can be automated but this is to demonstrate that the progam
    # accepts any input
    init_cons = []
    params = []

    confirm = ""
    while confirm not in ("y", "n"):
        confirm = input("Run this model? (y/n)\n> ")
    if confirm == "n":
        continue

    print("\nEnter <initial value, parameter> pairs separated by ','")
    for i, variable in enumerate(VarList):
        iv_param = list(map(float, input("> %c: " % variable).split(',')))
        init_cons.append(iv_param[0])
        params.append(iv_param[1])

    print("\nRunning ODEint...", end='')
    result = odeint(odeFunc, init_cons, t_range, args=(model,params))
    print(" done.")

    print("Plotting results...", end='')
    f = figure(ID)
    title(", ".join(IDtitle))
    for i, variable in enumerate(VarList):
        plot(t_range, result[:,i], label=variable)
    legend()
    axhline(0, color='k')
    savefig("model"+str(ID))
    close(f)
    print("done.")

print(" -- Bye --")