Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to read float numbers from a Binary file using Struct module and then storing them in numpy 3D arrays.When I run it as an independent script, it works fine.But when I call it as a class's function from another script(after import) it gives value error Here is my code.

import struct
from numpy import *
class DCD_read:
   def read_cord(self,total_atoms,dcd_data):
        cord_data=dcd_data[276:len(dcd_data)]        ##  binary data string
        byte=0
        count=0
       total_frames=info_dict['nset']
       coord=numpy.zeros((total_frames,total_atoms,3))         ## 3d array
       for frames in range(0,total_frames):
           for atoms in range(0,total_atoms):
              x = list(struct.unpack('<f',cord_data[60+byte:64+byte])) ###reading float
              byte+=4
              y = list(struct.unpack('<f',cord_data[60+byte:64+byte]))
              byte+=4
              z = list(struct.unpack('<f',cord_data[60+byte:64+byte]))
              byte+=4
              ls=x
              ls.extend(y)
              ls.extend(z)
              coord[frames][atoms]=ls
     return coord

Error:

Traceback (most recent call last):
File "C:\Users\Hira\Documents\PROJECT\md.py", line 24, in <module>
coord=dcd.read_cord(total_atoms,dcd_data)
File "C:\Users\Hira\Documents\PROJECT\DCD_read.py", line 51, in read_cord
coord=numpy.zeros((total_frames,total_atoms,3))
File "C:\Python27\numpy\core\numeric.py", line 148, in ones
a = empty(shape, dtype, order)
ValueError: negative dimensions are not allowed

md.py is the main (calling script) while DCD_read.py is module. Here is code for md.py (main script)

from DCD_read import *
import numpy
dcd_file=open('frame3.dcd',"rb")
dcd_data=dcd_file.read()
dcd=read_dcd()
total_atoms=6141
coord=dcd.read_cord(total_atoms,dcd_data)

Please can any one help???? I hope I explained it completely and clearly.Thanx

share|improve this question
    
Where is info_dict defined? Check the value of info_dict['nset'] (which you assign to total_frames). –  Warren Weckesser Sep 17 '13 at 17:57
    
info_dict is a global dictionary which is initialized and assigned in another function of the same class. info_dict['nset'] value can be in hundreds or thousands –  ZBatool Sep 18 '13 at 4:47
    
@ZBatool I would suggest you to put info_dict as an attribute of your class and avoid the global variable to see if the strange behavior will still happens... –  Saullo Castro Sep 19 '13 at 20:18
    
If you're trying to read dcd files from molecular dynamics simulations, you could try using github.com/rmcgibbo/mdtraj –  Robert T. McGibbon Oct 26 '13 at 10:12
    
side comment: please avoid using from ... import *. This is bad practice (but for very specific cases). –  Juh_ Jan 9 '14 at 10:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.