Skip to content

Commit 3d3a01f

Browse files
committed
add parseheader and getDataset
1 parent 3138546 commit 3d3a01f

1 file changed

Lines changed: 45 additions & 19 deletions

File tree

emi.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@ class fileEMI:
2121
A pathlib.Path object for the open file
2222
fid : file
2323
The file handle to the opened file.
24-
dataType : np.dtype
24+
data_type : list of np.dtype
2525
The numpy dtype of the data.
26-
dataSize : np.ndarray
27-
The size of the data in pixels.
26+
image_size : list of 2-tuples
27+
The size of the images in pixels.
28+
image_locations : list of int
29+
The file bytes locations of the images in the file.
30+
image_name : list of str
31+
The names of the images in the file.
2832
"""
2933

3034
_text_dtype = np.dtype([('mark','<u2'),('unknown','<u2'),('size','<u4')])
3135

3236
def __init__(self, file_name, verbose=False):
3337

34-
self.dataType = None
35-
self.dataSize = None
38+
self.data_type = []
39+
self.image_size = []
40+
self.image_locations = []
41+
self.image_name = []
3642
self._verbose = verbose
3743

3844
if hasattr(file_name, 'read'):
@@ -60,10 +66,10 @@ def __init__(self, file_name, verbose=False):
6066
except:
6167
raise
6268

63-
def _read_text(self,):
64-
aa = np.fromfile(self.fid, dtype=self._text_dtype, count=1)
69+
def _read_text(self, fid):
70+
aa = np.fromfile(fid, dtype=self._text_dtype, count=1)
6571
if aa['size'] > 0:
66-
bin = np.fromfile(self.fid, dtype='<u1', count=aa['size'][0])
72+
bin = np.fromfile(fid, dtype='<u1', count=aa['size'][0])
6773
text = ''.join([chr(item) for item in bin])
6874
else:
6975
text = ''
@@ -101,34 +107,54 @@ def parse_file(self):
101107
for loc in obj_loc[0:]:
102108
f0.seek(loc, 0)
103109
cur_text = self._read_text(f0)
110+
self.image_name.append(cur_text)
104111
if self._verbose:
105112
print(cur_text)
106-
second_field = np.fromfile(f0,dtype='<u2',count=1)
113+
second_field = np.fromfile(f0, dtype='<u2', count=1)
107114
if (second_field == 112) or (second_field == 17184):
108115
obj_info = np.fromfile(f0, count=2, dtype='<u2')
109116
if obj_info[0] == 1042:
110-
#this is the data. Read header and quit
117+
# this is an image. Read header and continue
111118
image_info = np.fromfile(f0, count=10, dtype='<u2')
112119
f0.seek(-8, 1);
113-
im_size = np.fromfile(f0,count=2,dtype='<u4')
120+
self.image_size.append(np.fromfile(f0,count=2,dtype='<u4'))
114121
if image_info[3] == 8710:
115-
numType = '<u2'
122+
self.data_type.append('<u2')
116123
elif image_info[3] == 8714:
117-
numType = '<u4'
124+
self.data_type.append('<u4')
118125
elif image_info[3] == 514:
119-
numType = '<u4'
126+
self.data_type.append('<u4')
120127
elif image_info[3] == 8716:
121-
numType = 'f32'
128+
self.data_type.append('f32')
122129
else:
123130
print('Unknown data type: {}'.format(image_info[3]))
124131
print('for object named: {}'.format(cur_text))
125132
return
133+
self.image_locations.append(self.fid.tell())
126134

127-
image = np.fromfile(f0, count=im_size[0]*im_size[1], dtype=numType)
128-
print('Only read first image named: {}'.format(cur_text))
129-
print(image)
130-
return image.reshape(im_size)
131135
f0.seek(-2, 1) # roll back the pointer 2 bytes
136+
137+
def getDataset(self, index=0):
138+
"""Read the data from the file
139+
140+
Paremeters
141+
----------
142+
index : int, optional
143+
The index of the image to load.
144+
145+
Returns
146+
-------
147+
: dict
148+
A dictionary contraining the data with the key 'data'
149+
150+
"""
151+
self.fid.seek(self.image_locations[index])
152+
image_size = self.image_size[index]
153+
dtype = self.data_type[index]
154+
image = np.fromfile(self.fid, count=image_size[0]*image_size[1], dtype=self.data_type)
155+
print('Only read first image named: {}'.format(cur_text))
156+
print(image)
157+
return image.reshape(im_size)
132158

133159
def emiReader(fname):
134160
full_file = np.fromfile(fname,dtype='<u1')

0 commit comments

Comments
 (0)