Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python "IndexError:"
#1
Dear experts,

I am not an expert in python. I am using a python code which takes two parameters Kpoints and Energy from a file EIGENVAL file. When I run the code it gives error message:

Traceback (most recent call last):
File "Fermi_surface.py", line 163, in <module>
kpoints[kp_counter,:]=num_temp
IndexError: index 46656 is out of bounds for axis 0 with size 46656

I am requesting if you can help me to solve this error.

I used python3.7 to compile the code.

I look forward to receiving your response.

Thanking you,

BaidyaS
Quote
#2
If I understand correctly, and IndexError for being out of range is that you have a dictionary somewhere with a certain number of spaces, but you are trying to fill a space that isn't there. eg, you have a book with 10 pages and you tell python to write on the 11th page.

Every time I've seen this problem is when someone (like myself lol) doesn't know or forgets that python starts counting at 0, not 1. You if you have 46655 spaces, they get numbered "0, 1, 2,..., 46654".

Without seeing the code, I'm going to guess that somewhere a range is set with something like range(46655) that can be changed to something like range(1, 46655) so it starts counting at 1 and not 0.

eg
for i in range(10):
    print(i)

for i in range(1, 10):
    print(i)
Quote
#3
indexes in python are zero-based. With 46656 elements max index would be 46655
Quote
#4
Dear machael1789,

Thanks for your reply. Here is the segment of the code where problem might arise as in the error message the line number 163 indicates.

# For loop goes to line by line of EIGENVAL file (afer 8th line), and record kpoints and eigenvalues
# depending on how many values are given. If # values are 4, it is for k_point,
# otherwise it is eigen value file.
for i,line in enumerate(lines[7::]): #starts from 8th line
# If the line is for k-point mash
if len(re.findall("[-a-zA-Z0-9.\+]+",line))==4:
temp=re.findall('[-a-zA-Z0-9.\+]+',line)
num_temp=[float(j) for j in temp]
kpoints[kp_counter,:]=num_temp
kp_counter+=1

# If the line is for Eigenvalues
elif len(re.findall("[-a-zA-Z0-9.\+]+",line)) > 0:
temp=re.findall('[-a-zA-Z0-9.\+]+',line)
band_num=int(temp[0])
ei_val_string=temp[1:1+ispin:]
ei_val=[float(k) for k in ei_val_string] #Eigenvlue of current Kpoint
eigenvalues[kp_counter-1,band_num-1,:]=ei_val

in_file.close()

If you would like to see the entire code I can attach here too.

Please let me know where is that index issue here.

Thanking you,

BaidyaS

Thank you. I have just found the problem. It looks like as you said there was space problem in the input file. Now it works.

Thanks.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)