Skip to content

Commit

Permalink
Merge pull request #5 from ashirsch/metadata
Browse files Browse the repository at this point in the history
Fix Metadata Reading
  • Loading branch information
ashirsch authored Jan 8, 2018
2 parents 8dbdcf1 + 91fc3f9 commit 7724dd1
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions spe2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, filepath=None):

self.xcoord, self.ycoord = self._get_coords()

self.data = self._read_data(file)
self.data, self.metadata, self.metanames = self._read_data(file)
file.close()

@staticmethod
Expand Down Expand Up @@ -103,6 +103,34 @@ def _get_dtype(file):

return dtype

def _get_meta_dtype(self):
meta_types = []
meta_names = []
prev_item = None
for item in dir(self.footer.SpeFormat.MetaFormat.MetaBlock):
if item == 'TimeStamp' and prev_item != 'TimeStamp': # Specify ExposureStarted vs. ExposureEnded
for element in self.footer.SpeFormat.MetaFormat.MetaBlock.TimeStamp:
meta_names.append(element['event'])
meta_types.append(element['type'])
prev_item = 'TimeStamp'
elif item == 'GateTracking' and prev_item != 'GateTracking': # Specify Delay vs. Width
for element in self.footer.SpeFormat.MetaFormat.MetaBlock.GateTracking:
meta_names.append(element['component'])
meta_types.append(element['type'])
prev_item = 'GateTracking'
elif prev_item != item: # All other metablock names only have one possible value
meta_names.append(item)
meta_types.append(getattr(self.footer.SpeFormat.MetaFormat.MetaBlock, item)['type'])
prev_item = item

for index, type_str in enumerate(meta_types):
if type_str == 'Int64':
meta_types[index] = np.int64
else:
meta_types[index] = np.float64

return meta_types, meta_names

def _get_roi_info(self):
"""
Returns region of interest attributes and numbers of regions of interest
Expand Down Expand Up @@ -147,7 +175,7 @@ def _get_dims(self):
xdim = [int(block["width"]) for block in self.footer.SpeFormat.DataFormat.DataBlock.DataBlock]
ydim = [int(block["height"]) for block in self.footer.SpeFormat.DataFormat.DataBlock.DataBlock]

return (xdim, ydim)
return xdim, ydim

def _get_coords(self):
"""
Expand Down Expand Up @@ -179,6 +207,16 @@ def _read_data(self, file):
"""
file.seek(4100)

frame_stride = int(self.footer.SpeFormat.DataFormat.DataBlock['stride'])
frame_size = int(self.footer.SpeFormat.DataFormat.DataBlock['size'])
metadata_size = frame_stride - frame_size
if metadata_size != 0:
metadata_dtypes, metadata_names = self._get_meta_dtype()
metadata = np.zeros((self.nframes, len(metadata_dtypes)))
else:
metadata_dtypes, metadata_names = None, None
metadata = None

data = [[0 for _ in range(self.nroi)] for _ in range(self.nframes)]
for frame in range(0, self.nframes):
for region in range(0, self.nroi):
Expand All @@ -189,7 +227,11 @@ def _read_data(self, file):
data_xdim = np.asarray(self.xdim[region], np.uint32)
data_ydim = np.asarray(self.ydim[region], np.uint32)
data[frame][region] = np.fromfile(file, self.dtype, data_xdim * data_ydim).reshape(data_ydim, data_xdim)
return data
if metadata_dtypes is not None:
for meta_block in range(len(metadata_dtypes)):
metadata[frame, meta_block] = np.fromfile(file, dtype=metadata_dtypes[meta_block], count=1)

return data, metadata, metadata_names

def image(self, frame=0, roi=0):
"""
Expand Down

0 comments on commit 7724dd1

Please sign in to comment.