Anja :
I am working with brain MRI data and it is .float data.
Do you know how to work with it in python?
with
with open('[43x25520].float') as f:
read_data = f.read()
I get:
Out[16]: Traceback (most recent call last):
File "<ipython-input-18-64e280c91de5>", line 2, in <module>
read_data = f.read()
File "/home/anja/anaconda3/lib/python3.7/codecs.py", line 322, in
decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position
2: invalid start byte
But I want to work with the floats in the file.
Holloway :
Assuming these files are just streams of floats, and they're small enough to fit in memory, the following should work.
from struct import iter_unpack
with open('/path/to/file', 'rb') as dat:
# This will give you your data as a 1D array
data = list(iter_unpack('f', dat.read()))
This assumes native endianess. You can change the 'f'
to '<f'
or '>f'
if you need to specify something different. See here.
To convert it to a correctly dimensioned matrix, I'd look at numpy.
import numpy as np
matrix = np.array(data).reshape(43, 25520) # data from above
print(matrix[2,4523])
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=319673&siteId=1