简体   繁体   中英

Python: An error occurs when my loop is running for the second time

I have some trouble with this code. I am running this piece of code for one value of a constant b and for two values of a constant a in a loop. When I'm running the code without the last line t_c.append([au.link_q(f) for f in F2]) it works perfectly. When I'm introducing the last line then the code is running as it should for the first value of a but something goes wrong when the code is running for the second time for the second value of a . Something tells me that the error then lays on the last line.

Here is what the program gives me when it tries to run for the second time in the loop.

  File "C:/Users/Samir Kozarcanin/Documents/Min kode python/EuropeanGridR/prove_ind_til_videre.py", line 87, in <module>
    foobar = np.array(ntimesn(xgauss))

  File "C:/Users/Samir Kozarcanin/Documents/Min kode python/EuropeanGridR/prove_ind_til_videre.py", line 57, in ntimesn
    res[u][v] = f(u, v, m)                                       #

TypeError: 'numpy.ndarray' object is not callable

But it's strange that it runs fine for the first value of a but not for the next one. `

def get_q(s,q=0.99):
""" Looks at a cumulative histogram of a time series.It
    returns the magnitude of s that happens a fraction 'quant' of the time.
"""
return mquant(s,q)[0]

def link_q(f,q=0.99):
    a = (1-q)/2.0
    b = q+a
    return max(-mquant(f,a)[0],mquant(f,b)[0])

def nodes_B(N = None,B=1,alpha=0.8):
    if N == None:
        N = EU_Nodes()
    LEU = np.sum(n.mean for n in N)
    CFW = np.sum(n.mean*n.cf_w**B for n in N)
    CFS = np.sum(n.mean*n.cf_s**B for n in N)
    for n in N:
        n.gw = n.cf_w**B *LEU / CFW
        n.gs = n.cf_s**B *LEU / CFS
        n.gamma = (alpha*n.gw+(1-alpha)*n.gs)*1.0
        n.alpha = alpha
        n._update_()
    return N

def f(s,p, matrix):
    res = 0  
    for x in xrange(len(matrix[0])):                                     
        res += matrix[s][x] * matrix[p][x]                               
    res = res/len(matrix[0])
    return res

def ntimesn(m):         
    res = [[0 for x in range(len(m))] for x in range(len(m))]
    for u in xrange(len(m)):
        for v in xrange(u, len(m)):
            res[u][v] = f(u, v, m)
            res[v][u] = res[u][v]
    return res

alphas=[0, 0.05]
betas = [0]
d=1
ii=range(30)
jj=range(30)
kk=range(30)
nul = [0] * 30

for b in betas:

    for a in alphas:
        xgauss=[]
        N=nodes_B(B=b, alpha=a)

        for index in ii:
            hist, x = np.histogram(N[index].mismatch[:10], 70128, normed=1)
            hist2 = hist*np.diff(x) 
            cumulative=np.cumsum(hist2) # laver cumulative værdier
            f1 = InterpolatedUnivariateSpline(x[:-1], cumulative, k=3)
            Fdelta=f1(N[index].mismatch) 
            xax=np.arange(-4,4,0.1)
            Fgauss=(1+erf(xax/(math.sqrt(2))))/2 
            g1 = InterpolatedUnivariateSpline(Fgauss, xax, k=3) 
            xgauss.append(g1(Fdelta))
        foobar = np.array(ntimesn(xgauss))

        for i in ii:
            for j in jj:
                if i!=j:
                    foobar[i,j] = foobar[i,j]*d       

        xgx = InterpolatedUnivariateSpline(xax,Fgauss, k=3) 
        nul = [0] * 30
        randxgauss=[]
        for i in xrange(70128):
            newvalues=(numpy.random.multivariate_normal(nul, foobar))
            randxgauss.append(xgx(newvalues))

        randxgauss = np.array(randxgauss)


        delta=[]
        kk=range(30)
        for k in kk:
            hist, x = np.histogram(N[k].mismatch, 70128, normed=1)
            hist2 = hist*np.diff(x) 
            cumulative=np.cumsum(hist2)
            delta.append(interp(randxgauss[:,k], cumulative, x[:-1]))

        t_c=[]
        for n in N:
            n.mismatch = delta[n.id][:]
        N2,F2 = au.solve(N,mode="copper linear verbose",lapse=100)

        t_c.append([au.link_q(f) for f in F2])`

It seems that you have overwritten t_c.append. At some point in your code, you may have written

t_c.append = np.ndarray(["example ", "array"])

Then it tries to call the uncallable np.ndarray() at t_c.append([...]) . To fix this, use t_c.append(np.ndarray([...]), ...) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM