From 32710192ad1c94a5f0932fa64251285e8e1e3abe Mon Sep 17 00:00:00 2001 From: "donald e. boyce" Date: Tue, 9 Jan 2024 14:45:34 -0500 Subject: [PATCH 1/2] imagefiles loads from string --- hexrd/imageseries/load/imagefiles.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/hexrd/imageseries/load/imagefiles.py b/hexrd/imageseries/load/imagefiles.py index d7402f8d5..6bf686406 100644 --- a/hexrd/imageseries/load/imagefiles.py +++ b/hexrd/imageseries/load/imagefiles.py @@ -25,13 +25,15 @@ class ImageFilesImageSeriesAdapter(ImageSeriesAdapter): format = 'image-files' - def __init__(self, fname, **kwargs): + def __init__(self, fname): """Constructor for image files image series - *fname* - should be yaml file with files and metadata sections - *kwargs* - keyword arguments - . 'files' = a list of image files - . 'metadata' = a dictionary + Parameters + ---------- + fname: string | path + name of YAML file or bytestring of YAML contents + + """ self._fname = fname self._load_yml() @@ -81,13 +83,25 @@ def __str__(self): self.dtype, self.shape, self.singleframes) return s + @property + def fname(self): + return self._fname + def _load_yml(self): EMPTY = 'empty-frames' MAXTOTF = 'max-total-frames' MAXFILF = 'max-file-frames' DTYPE = 'dtype' - with open(self._fname, "r") as f: - d = yaml.safe_load(f) + # + # Check whether fname is a filename or YAML content. If it has multiple + # lines, we consider it to be content, otherwise a file name. + # + nlines = len(self.fname.splitlines()) + if nlines > 1: + d = yaml.safe_load(self.fname) + else: + with open(self._fname, "r") as f: + d = yaml.safe_load(f) imgsd = d['image-files'] dname = imgsd['directory'] fglob = imgsd['files'] From d56dfa304f089c97b6bb4d0ba73ce98f19a7340c Mon Sep 17 00:00:00 2001 From: "donald e. boyce" Date: Sun, 14 Jan 2024 11:01:31 -0500 Subject: [PATCH 2/2] imagefiles: fixed case when fname is path --- hexrd/imageseries/load/imagefiles.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hexrd/imageseries/load/imagefiles.py b/hexrd/imageseries/load/imagefiles.py index 6bf686406..6f954f996 100644 --- a/hexrd/imageseries/load/imagefiles.py +++ b/hexrd/imageseries/load/imagefiles.py @@ -30,10 +30,8 @@ def __init__(self, fname): Parameters ---------- - fname: string | path + fname: string | Path name of YAML file or bytestring of YAML contents - - """ self._fname = fname self._load_yml() @@ -93,15 +91,18 @@ def _load_yml(self): MAXFILF = 'max-file-frames' DTYPE = 'dtype' # - # Check whether fname is a filename or YAML content. If it has multiple - # lines, we consider it to be content, otherwise a file name. + # Check whether fname is a pathlib Path, a filename or YAML content. + # If it has multiple lines, we consider it to be YAML content, + # otherwise a file name. # - nlines = len(self.fname.splitlines()) + is_str = isinstance(self.fname, str) + nlines = len(self.fname.splitlines()) if is_str else 1 if nlines > 1: d = yaml.safe_load(self.fname) else: with open(self._fname, "r") as f: d = yaml.safe_load(f) + imgsd = d['image-files'] dname = imgsd['directory'] fglob = imgsd['files']