0

I am working with a large simulation code and am having a hard time executing some iteration. I have imported data of size 262144 from a text file and reshaped into a (64,64,64) set as such

    filename = path + 'density'
    file = open(filename, mode='rb')
    bin_array = array.array('f')
    bin_array.fromfile(file, 64 * 64 * 64)
    rho = np.reshape(bin_array, (64,64,64))

This is then stored in self.data as:

     self.data = [rho]

and accessed using self.data[0][x,y,z]. According to the manual for the code I'm working with, each value in rho needs to be a float of 4 bytes. Now when I run other working examples (call the data for this 'good_data'), the data used gives the following output for isinstance:

    isinstance(good_data, float) 
    >> True

Now when I use isinstance on rho, it returns false and I believe this is whats giving me problems. Can anyone tell me how I can have my data return true for isinstance? Essentially what I want is

    isinstance(rho[x,y,z], float)
    >> True

When I check the dtype it returns float32 but returns false for isinstance. Thank you all!

APMATH24
  • 1
  • 1
  • Each *value* is a `float`, that however does not make `rho` itself a `float`, since it is an array of floats. – Willem Van Onsem Jul 24 '19 at 21:50
  • how do you suggest I convert rho into a float – APMATH24 Jul 24 '19 at 22:00
  • A `float` is a single floating-point number. You can't make an array of floats into a single float, or it ceases to be an array. You say that in the documentation, "each value in rho needs to be a float", so it is explicitly asking for an array of floats, not a single float. – G. Anderson Jul 24 '19 at 22:04
  • Is there a way for me to put a value of rho into isintance and return true for float? Because when I check dtype it says float32 but when i use isinstance it returns false – APMATH24 Jul 24 '19 at 22:08
  • This is sounding like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Is there a specific reason you need the built-in `isinstance()` function to return the wrong type? Or are you open to solutions such as `isinstance(rho[0,0,0], float)` or `if rho.dtype==float32: return True`? – G. Anderson Jul 24 '19 at 22:13
  • in a nutshell my problem is I am getting a TypeError: 'float' object is not subscriptable error. This error comes from another portion of the simulation code trying to access rho. I don't understand why I get this error as I am no python expert, but I noticed that if I am using the codes built in example, the data type returns true for isinstance and so I feel like that should fix things. – APMATH24 Jul 24 '19 at 22:15

0 Answers0