0

I have some weird problem while reading file with Python3. With Matlab I have code

fid = fopen('histograms_testi.bin','r');    
data = fread(fid,'*uint16','ieee-be');
fclose(fid);

%reshaping 

data = reshape(data,4096,numel(data)/4096);

where the data is now some matrix of shape (10127 , 4096 ) which is fine (later I'm reshaping it to 10100x 4096 form)

With Python3 I tried to read the same file using different methods, i.e.

data = np.fromfile('histograms_testi.bin', dtype='>f8', count=-1)

that was given in Reading binary big endian files in python . Unfortunately this then gives error of

ValueError: cannot reshape array of size 10370048 into shape (2531,4096)


The way I got this working with Python3 is following

data= np.fromfile('histograms_testi.bin', 'uint16')

and I can easily reshape it into the shape I want

rs = data.reshape(int(len(data)/4096),4096)
rs = rs[:10100]


Now the problem/weird part is that, the scale on the y-axis is completely different. (x-axis seems completely same).

The maximum value of the first data 'row' using matlab (data(:,1)) is 118, while with the Python it is 30208.

I am a bit worried, that the results will not be completely correct, eventhough the shape of the figures are same (scale on y-axis are not).

Any ideas?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
mamark
  • 3
  • 3
  • I forgot to mention, that the 'histogram_testi.bin' is in big-endian and uint16 format – mamark Jun 25 '20 at 08:55
  • According to [Data type objects (dtype)](https://numpy.org/doc/1.18/reference/arrays.dtypes.html), the character `>` indicates a big endian ordering, but the `f8` means an 8 bytes `float`. Not the best to read integers. I would try `dtype='>u2'` – Hoki Jun 25 '20 at 09:08
  • @Hoki Thank you so so much! This seems to work now completely similarly as with Matlab! – mamark Jun 25 '20 at 09:26

0 Answers0