From cabbd79fbfc88681dc5590e7272caba61adc0455 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 11 Feb 2020 14:54:07 -0500 Subject: [PATCH 1/8] ENH: handler for hdf5 files that have variable frames per point --- area_detector_handlers/handlers.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index 862a241..ed36805 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -21,6 +21,50 @@ class IntegrityError(Exception): pass +class HDF5VariableFramesHandler(HandlerBase): + """ + Handler for data stored in one Dataset of an HDF5 file. + + Parameters + ---------- + filename : string + path to HDF5 file + swmr : bool, optional + Open the hdf5 file in SWMR read mode. Only used when mode = 'r'. + Default is False. + """ + + specs = {"AD_HDF5_v1"} | HandlerBase.specs + + def __init__(self, filename): + self._filename = filename + self._key = "/entry/data/data" + self._file = None + self._dataset = None + self.open() + + def get_file_list(self, datum_kwarg_gen): + return [self._filename] + + def __call__(self, offset, num_frames): + return self._dataset[offset : offset + num_frames] + + def open(self): + import h5py + + if self._file: + return + + self._file = h5py.File(self._filename, "r") + self._dataset = self._file[self._key] + + def close(self): + super(HandlerBase, self).close() + self._dataset = None + self._file.close() + self._file = None + + class AreaDetectorSPEHandler(HandlerBase): specs = {"AD_SPE"} | HandlerBase.specs From c96ab3756ebf6430372023f4f2707e2763699888 Mon Sep 17 00:00:00 2001 From: Maksim Rakitin Date: Tue, 11 Feb 2020 18:02:11 -0500 Subject: [PATCH 2/8] Use Py3+ --- area_detector_handlers/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index ed36805..3c6305b 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -59,7 +59,7 @@ def open(self): self._dataset = self._file[self._key] def close(self): - super(HandlerBase, self).close() + super().close() self._dataset = None self._file.close() self._file = None From dd232a6c9368e3a0fc005966db399fecacb5349f Mon Sep 17 00:00:00 2001 From: Maksim Rakitin Date: Thu, 13 Feb 2020 17:45:41 -0500 Subject: [PATCH 3/8] STY: flake8 --- area_detector_handlers/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index 3c6305b..c60851a 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -47,7 +47,7 @@ def get_file_list(self, datum_kwarg_gen): return [self._filename] def __call__(self, offset, num_frames): - return self._dataset[offset : offset + num_frames] + return self._dataset[offset: offset + num_frames] def open(self): import h5py From e5665d1e8a370e36a5614b796c9c36a7cc88bf88 Mon Sep 17 00:00:00 2001 From: FXI Operator Date: Wed, 19 Feb 2020 14:50:01 -0500 Subject: [PATCH 4/8] ENH: add timestamp handler as well --- area_detector_handlers/handlers.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index c60851a..0b1aa68 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -65,6 +65,26 @@ def close(self): self._file = None +class HDF5VariableFramesHandlerTS(HDF5VariableFramesHandler): + def open(self): + import h5py + + if self._file: + return + + self._file = h5py.File(self._filename, "r") + self._dataset1 = self._file["/entry/instrument/NDAttributes/NDArrayEpicsTSSec"] + self._dataset2 = self._file["/entry/instrument/NDAttributes/NDArrayEpicsTSnSec"] + + + def __call__(self, offset, num_frames): + # Don't read out the dataset until it is requested for the first time. + start, stop = offset, offset + num_frames + rtn = self._dataset1[start:stop].squeeze() + rtn = rtn + (self._dataset2[start:stop].squeeze() * 1e-9) + return rtn + + class AreaDetectorSPEHandler(HandlerBase): specs = {"AD_SPE"} | HandlerBase.specs From b9649d6e7f3c2f81a4f53a1a0d61d3f166043927 Mon Sep 17 00:00:00 2001 From: Dan Allan Date: Wed, 26 Aug 2020 11:10:47 -0400 Subject: [PATCH 5/8] whitespace --- area_detector_handlers/handlers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index 0b1aa68..f77c9ee 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -76,13 +76,12 @@ def open(self): self._dataset1 = self._file["/entry/instrument/NDAttributes/NDArrayEpicsTSSec"] self._dataset2 = self._file["/entry/instrument/NDAttributes/NDArrayEpicsTSnSec"] - def __call__(self, offset, num_frames): # Don't read out the dataset until it is requested for the first time. start, stop = offset, offset + num_frames rtn = self._dataset1[start:stop].squeeze() rtn = rtn + (self._dataset2[start:stop].squeeze() * 1e-9) - return rtn + return rtn class AreaDetectorSPEHandler(HandlerBase): From 4f757cc251951c54f0ae4e41ccec0fe54bbf8532 Mon Sep 17 00:00:00 2001 From: Dan Allan Date: Wed, 26 Aug 2020 11:13:04 -0400 Subject: [PATCH 6/8] Add specs to timestamp handler. --- area_detector_handlers/handlers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index f77c9ee..4a3337b 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -66,6 +66,8 @@ def close(self): class HDF5VariableFramesHandlerTS(HDF5VariableFramesHandler): + specs = {"AD_HDF5_TS_v1"} | HandlerBase.specs + def open(self): import h5py From 58264e57ccd6bacbec31949da0db0220252b7408 Mon Sep 17 00:00:00 2001 From: Dan Allan Date: Wed, 26 Aug 2020 11:13:15 -0400 Subject: [PATCH 7/8] Add entrypoints for variable frames handlers. --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index 0b4ec3b..2b0852f 100644 --- a/setup.py +++ b/setup.py @@ -52,8 +52,10 @@ "AD_EIGER2 = area_detector_handlers.eiger:EigerHandler", "AD_EIGER_SLICE = area_detector_handlers.eiger:EigerHandler", "AD_HDF5 = area_detector_handlers.handlers:AreaDetectorHDF5Handler", + "AD_HDF5_v1 = area_detector_handlers.handlers:HDF5VariableFramesHandler", "AD_HDF5_SWMR = area_detector_handlers.handlers:AreaDetectorHDF5SWMRHandler", "AD_HDF5_TS = area_detector_handlers.handlers:AreaDetectorHDF5TimestampHandler", + "AD_HDF5_TS_v1 = area_detector_handlers.handlers:HDF5VariableFramesHandlerTS", "AD_HDF5_SWMR_TS = area_detector_handlers.handlers:AreaDetectorHDF5SWMRTimestampHandler", "AD_HDF5_SINGLE = area_detector_handlers.handlers:AreaDetectorHDF5SingleHandler", "SPECS_HDF5_SINGLE_DATAFRAME = area_detector_handlers.handlers:SpecsHDF5SingleHandlerDataFrame", From 47668bed29c9aeba6ae3b69329551f54b51a7978 Mon Sep 17 00:00:00 2001 From: Maksim Rakitin Date: Wed, 26 Aug 2020 12:11:22 -0400 Subject: [PATCH 8/8] Make the open method for HDF5VariableFramesHandler(s) private --- area_detector_handlers/handlers.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/area_detector_handlers/handlers.py b/area_detector_handlers/handlers.py index 4a3337b..1e5be30 100644 --- a/area_detector_handlers/handlers.py +++ b/area_detector_handlers/handlers.py @@ -41,7 +41,7 @@ def __init__(self, filename): self._key = "/entry/data/data" self._file = None self._dataset = None - self.open() + self._open() def get_file_list(self, datum_kwarg_gen): return [self._filename] @@ -49,9 +49,7 @@ def get_file_list(self, datum_kwarg_gen): def __call__(self, offset, num_frames): return self._dataset[offset: offset + num_frames] - def open(self): - import h5py - + def _open(self): if self._file: return @@ -68,9 +66,7 @@ def close(self): class HDF5VariableFramesHandlerTS(HDF5VariableFramesHandler): specs = {"AD_HDF5_TS_v1"} | HandlerBase.specs - def open(self): - import h5py - + def _open(self): if self._file: return