Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleans up rawimage _get_length() and __get_item__() functions #610

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 25 additions & 38 deletions hexrd/imageseries/load/rawimage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" Adapter class for raw image reader
"""
import warnings
""" Adapter class for raw image reader"""
import os

import numpy as np
import yaml
Expand All @@ -17,8 +16,10 @@ class RawImageSeriesAdapter(ImageSeriesAdapter):
def __init__(self, fname, **kwargs):
"""Image data in custom format

*fname* - filename of the data file
*kwargs* - keyword arguments (none required)
Parameters
----------
fname: string or Path
name of input YAML file describing the format
"""
self.fname = fname
with open(fname, "r") as f:
Expand All @@ -27,14 +28,15 @@ def __init__(self, fname, **kwargs):
self.fname = y['filename']
self.dtype = self._get_dtype(y['scalar'])
self._shape = tuple((int(si) for si in y['shape'].split()))
self.framesize = self._shape[0]*self._shape[1]
self._frame_size = self._shape[0] * self._shape[1]
self._frame_bytes = self._frame_size * self.dtype.itemsize
self.skipbytes = y['skip']
self._len = self._get_length()
self._meta = dict()
self.kwargs = kwargs

# Prepare to read
self.iframe = -1
# Open file for reading.
self.f = open(self.fname, "r")

def _get_dtype(self, scalar):
numtype = scalar['type']
Expand All @@ -51,19 +53,18 @@ def _get_dtype(self, scalar):

def _get_length(self):
"""Read file and determine length"""
iframe = 0
with open(self.fname, "r") as f:
_ = np.fromfile(f, np.byte, count=self.skipbytes)
# now keep reading frames until EOF
moreframes = True
while moreframes:
_ = np.fromfile(f, self.dtype, count=self.framesize)
if len(_) == self.framesize:
iframe += 1
else:
moreframes = False

return iframe
nbytes = os.path.getsize(self.fname)
if nbytes % self._frame_bytes == self.skipbytes:
nframes = (nbytes - self.skipbytes) // self._frame_bytes
else:
msg = (
f"Total number of bytes ({nbytes}) does not work with "
f"skipbytes ({self.skipbytes}) and "
f"_frame_size ({self._frame_size}). Check the skipbytes value."
)
raise ValueError(msg)

return nframes

@staticmethod
def typechars(numtype, bytes_=4, signed=False, little=True):
Expand Down Expand Up @@ -103,23 +104,9 @@ def __iter__(self):
return ImageSeriesIterator(self)

def __getitem__(self, key):
if self.iframe < 0:
self.f = open(self.fname, "r")
_ = np.fromfile(self.f, np.byte, count=self.skipbytes)
self.iframe = 0
if key == 0:
self.f.seek(0, 0)
_ = np.fromfile(self.f, np.byte, count=self.skipbytes) #dcp fix to make sure bytes are properly skipped
self.iframe = 0
if key != self.iframe:
msg = "frame %d not available, series must be read in sequence!"
raise ValueError(msg % key)
frame = np.fromfile(self.f, self.dtype, count=self.framesize)
self.iframe += 1
if self.iframe == len(self):
self.iframe = -1
self.f.close()

count = key * self._frame_bytes + self.skipbytes
self.f.seek(count, 0)
frame = np.fromfile(self.f, self.dtype, count=self._frame_size)
return frame.reshape(self.shape)

@property
Expand Down
Loading