AA16
AA16

Reputation: 43

IndexError: too many indices for array for an array that is definitely as big

I'm trying to make a movie by taking png images of an updating plot and stitching them together. There are three variables: degrees, ksB, and mp. Only mp changes each frame; the other two are constant. The data for mp for all times is stored in X. This is the relevant part of the code:

def plot(fname, haveMLPY=False):
    # Load data from .npz file.
    data = np.load(fname)
    X = data["X"]
    T = data["T"]
    N = X.shape[1]
    A = data["vipWeights"]
    degrees = A.sum(1)
    ksB = data["ksB"]

    # Initialize a figure.
    figure = plt.figure()

    # Generate a plottable axis as the first subplot in 1 rows and 1 columns.
    axis = figure.add_subplot(1,1,1)

    # MP is the first (0th) variable. Plot one trajectory for each cell over time.
    axis.plot(T, X[:,:,0], color="black")

    # Decorate the plot.
    axis.set_xlabel("time [hours]")
    axis.set_ylabel("MP [nM]")
    axis.set_title("PER mRNA concentration across all %d cells" % N)
    firstInd = int(T.size / 2)

    if haveMLPY:
        import circadian.analysis
        # Generate a and plot Signal object, which encapsulates wavelet analysis.
        signal = circadian.analysis.Signal(X[firstInd:, 0, 0], T[firstInd:])
        signal.showSpectrum(show=False)


    files=[]   
    # filename for the name of the resulting movie
    filename = 'animation'
    mp = X[10**4-1,:,0]
    from mpl_toolkits.mplot3d import Axes3D  
    for i in range(10**4):
        print i
        mp = X[i,:,0]
        data2 = np.c_[degrees, ksB, mp]

        # Find best fit surface for data2
        # regular grid covering the domain of the data
        mn = np.min(data2, axis=0)
        mx = np.max(data2, axis=0)
        X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))
        XX = X.flatten()
        YY = Y.flatten()
        order = 2    # 1: linear, 2: quadratic
        if order == 1:
            # best-fit linear plane
            A = np.c_[data2[:,0], data2[:,1], np.ones(data2.shape[0])]
            C,_,_,_ = scipy.linalg.lstsq(A, data2[:,2])    # coefficients

            # evaluate it on grid
            Z = C[0]*X + C[1]*Y + C[2]

            # or expressed using matrix/vector product
            #Z = np.dot(np.c_[XX, YY, np.ones(XX.shape)], C).reshape(X.shape)

        elif order == 2:
            # best-fit quadratic curve
            A = np.c_[np.ones(data2.shape[0]), data2[:,:2], np.prod(data2[:,:2], axis=1), data2[:,:2]**2]
            C,_,_,_ = scipy.linalg.lstsq(A, data2[:,2])

            # evaluate it on a grid
            Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX*YY, XX**2, YY**2], C).reshape(X.shape)

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)
        ax.scatter(degrees, ksB, mp)
        ax.set_xlabel('degrees')
        ax.set_ylabel('ksB')
        ax.set_zlabel('mp')
        # form a filename
        fname2 = '_tmp%03d.png'%i
        # save the frame
        savefig(fname2)
        # append the filename to the list
        files.append(fname2)
    # call mencoder 
    os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o " + filename + ".mpg")
    # cleanup
    for fname2 in files: os.remove(fname2)

Basically, all the data is stored in X. The format X[i, i, i] means X[time, neuron, data type]. Each time through the loop, I want to update the time, but still plot mp (the 0th variable) for all the neurons.

When I run this code, I get "IndexError: too many indices for array". I asked it to print i to see when the code was going wrong. I get an error when i = 1, meaning that the code loops through once but then has the error the second time.

However, I have data for 10^4 time steps. You can see in the first line of the provided code, I access X[10**4-1, :, 0] successfully. That's why it's confusing to me why X[1,:,0] would be out of range. If anybody could explain why/help me get around this, that would be great.

The traceback error is

Traceback (most recent call last):
File"/Users/angadanand/Documents/LiClipseWorkspace/Circadian/scripts    /runMeNets.py", line 196, in module  
plot(fname)  
File"/Users/angadanand/Documents/LiClipseWorkspace/Circadian/scripts    /runMeNets.py", line 142, in plot  
mp = X[i,:,0]  
IndexError: too many indices for array

Thanks!

Upvotes: 3

Views: 260

Answers (1)

MSeifert
MSeifert

Reputation: 152587

Your problem is that you overwrite your X inside your loop:

X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))

So afterwards it will have another shape and contain different data. I would suggest changing this second X to x_grid and check where you need this "other" X and where the original.

for example:

X_grid, Y_grid = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))

Upvotes: 3

Related Questions