0

I realize a lot of people have asked about this error, but I have yet to find anything that will help me.

This is my code:

def nanalyze(pupil, pw_sim):
    import numpy as np

    temp_s = abs(pw_sim)**2 * pupil

    vals_x, vals_y = np.where(pupil > 0)
    mask = pupil[vals_x[0]:vals_x[len(vals_x)-1], vals_y[0]:vals_y[len(vals_y)-1]]

    s_i = (np.mean(temp_s[tuple(mask)]**2) / (np.mean(temp_s[tuple(mask)])**2)) - 1
    return s_i

The second-to -last line,s_i = (np.mean(temp_s[tuple(mask)]**2) / (np.mean(temp_s[tuple(mask)])**2)) - 1, is what returns the index error: too many indices.

pupil and pw_sim are each arrays of shape (1024,1024). temp_s is therefore also a (1024,1024) array and mask ends up being a (1023,1023) array. I've tried making mask a (1024,1024) to check and see if I'm getting the index error just because of the difference in shapes, but that doesn't seem to change anything.

4
  • What is this expected to do: temp_s[tuple(mask)] ? Commented Jul 31, 2015 at 21:39
  • It's supposed to index temp_s using mask. I translated this whole thing from Matlab code that had it as temp_s(mask). Commented Jul 31, 2015 at 21:41
  • if you're creating a tuple - it can't be used as an index (to the best of my knowledge). Commented Jul 31, 2015 at 21:41
  • Hmm. I originally had it as just temp_s[mask] and I got a different error saying I couldn't use mask as an index. I actually found a different question on this site which said that could be rectified by using a tuple Commented Jul 31, 2015 at 21:45

2 Answers 2

0

You should probably read about numpy's advanced indexing, sometimes called "fancy indexing".

It's not clear from your question what you're trying to do with temp_s[mask]. If mask is really a mask, you'll need to make sure it has dtype bool and has the same shape as temp_s. If it's not a mask, you'll need to explain better what you're trying to do.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll look into that. mask is a mask in terms of the module I'm using, but it is not a mask that would have dtype bool. All I know about temp_s[mask] is that it's supposed to index temp_s using mask.
-1

It turns out that simplifying and using different syntax solved the problem. Now, rather than

vals_x, vals_y = np.where(pupil > 0)
mask = pupil[vals_x[0]:vals_x[len(vals_x)-1], vals_y[0]:vals_y[len(vals_y)-1]]

s_i = (np.mean(temp_s[tuple(mask)]**2) / (np.mean(temp_s[tuple(mask)])**2)) - 1

I have

temp_s_nz = np.mean(temp_s[pupil >0])
temp_sq_nz = np.mean(temp_s[pupil >0]**2)

s_i = (temp_sq_nz) / (temp_s_nz)**2 - 1

This change was made by my mentor, so I can't fully explain why it works, but I know that there are no longer any errors and the code is easier to read. I think this works because of the way pupil >0 is defined; in my original code there was room for error in it and now there is not. Thanks for all your help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.