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

Frame cache writer #605

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
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
65 changes: 52 additions & 13 deletions hexrd/imageseries/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,17 @@ def __init__(self, ims, fname, **kwargs):
self._fname_base = tmp[0]
self._fname_suff = tmp[1]

pass # end class
@property
def fname(self):
return self._fname

@property
def fname_dir(self):
return self._fname_dir

@property
def opts(self):
return self._opts

class WriteH5(Writer):
fmt = 'hdf5'
Expand Down Expand Up @@ -170,22 +179,48 @@ class WriteFrameCache(Writer):
def __init__(self, ims, fname, **kwargs):
"""write yml file with frame cache info

kwargs has keys:

cache_file - name of array cache file
meta - metadata dictionary
The default write option is to save image data and metadata all
in a single npz file. The original option was to have a YAML file
as the primary file and a specified cache file for the images; this
method is deprecated.

Parameters
----------
ims: Imageseries instance
the imageseries to write
fname: str or Path
name of file to write;
threshold: float
threshold value for image, at or below which values are zeroed
cache_file: str or Path, optional
name of the npz file to save the image data, if not given in the
`fname` argument; for YAML format (deprecated), this is required
"""
Writer.__init__(self, ims, fname, **kwargs)
self._thresh = self._opts['threshold']
cf = kwargs['cache_file']
if os.path.isabs(cf):
self._cache = cf
else:
cdir = os.path.dirname(fname)
self._cache = os.path.join(cdir, cf)
self._cachename = cf
self._cache, self.cachename = self._set_cache()
self.max_workers = kwargs.get('max_workers', None)

def _set_cache(self):

cf = self.opts.get('cache_file')

if cf is None:
cachename = cache = self.fname
else:
if os.path.isabs(cf):
cache = cf
else:
cdir = os.path.dirname(self.fname)
cache = os.path.join(cdir, cf)
cachename = cf

return cache, cachename

@property
def cache(self):
return self._cache

def _process_meta(self, save_omegas=False):
d = {}
for k, v in list(self._meta.items()):
Expand Down Expand Up @@ -273,7 +308,7 @@ def extract_data(i):
arrd['nframes'] = len(self._ims)
arrd['dtype'] = str(self._ims.dtype).encode()
arrd.update(self._process_meta())
np.savez_compressed(self._cache, **arrd)
np.savez_compressed(self.cache, **arrd)

def write(self, output_yaml=False):
"""writes frame cache for imageseries
Expand All @@ -282,4 +317,8 @@ def write(self, output_yaml=False):
"""
self._write_frames()
if output_yaml:
warnings.warn(
"YAML output for frame-cache is deprecated",
DeprecationWarning
)
self._write_yml()
12 changes: 11 additions & 1 deletion tests/imageseries/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ def test_fmtfc(self):
"""save/load frame-cache format"""
imageseries.write(self.is_a, self.fcfile, self.fmt,
threshold=self.thresh, cache_file=self.cache_file)
# is_fc = imageseries.open(self.fcfile, self.fmt, style='yml')
is_fc = imageseries.open(self.fcfile, self.fmt)
diff = compare(self.is_a, is_fc)
self.assertAlmostEqual(diff, 0., "frame-cache reconstruction failed")
self.assertTrue(compare_meta(self.is_a, is_fc))

def test_fmtfc_nocache_file(self):
"""save/load frame-cache format with no cache_file arg"""
imageseries.write(
self.is_a, self.fcfile, self.fmt,
threshold=self.thresh
)
is_fc = imageseries.open(self.fcfile, self.fmt)
diff = compare(self.is_a, is_fc)
self.assertAlmostEqual(diff, 0., "frame-cache reconstruction failed")
Expand Down