diff --git a/hexrd/imageseries/save.py b/hexrd/imageseries/save.py index 106c68b4b..59e223346 100644 --- a/hexrd/imageseries/save.py +++ b/hexrd/imageseries/save.py @@ -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' @@ -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()): @@ -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 @@ -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() diff --git a/tests/imageseries/test_formats.py b/tests/imageseries/test_formats.py index 7428a11e8..ae33bb8f8 100644 --- a/tests/imageseries/test_formats.py +++ b/tests/imageseries/test_formats.py @@ -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")