-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for qpimage.QPSeries hdf5 file format
- Loading branch information
1 parent
dceb568
commit db9f68d
Showing
3 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,61 @@ | ||
from dataset import DataSet | ||
import h5py | ||
import qpimage | ||
|
||
from .dataset import DataSet | ||
|
||
|
||
class SeriesHdf5Qpimage(DataSet): | ||
pass | ||
def __init__(self, *args, **kwargs): | ||
super(SeriesHdf5Qpimage, self).__init__(*args, **kwargs) | ||
self._qpseries = qpimage.QPSeries(h5file=self.path, | ||
h5mode="r") | ||
self._dataset = None | ||
|
||
def __len__(self): | ||
return len(self._qpseries) | ||
|
||
def get_qpimage(self, idx=0): | ||
"""Return background-corrected QPImage of data at index `idx`""" | ||
if self._bgdata: | ||
# The user has explicitly chosen different background data | ||
# using `get_qpimage_raw`. | ||
return super(SeriesHdf5Qpimage, self).get_qpimage(idx) | ||
else: | ||
# We can use the background data stored in the qpimage hdf5 file | ||
return self._qpseries.get_qpimage(index=idx).copy() | ||
|
||
def get_qpimage_raw(self, idx=0): | ||
"""Return QPImage without background correction""" | ||
qpi = self._qpseries.get_qpimage(index=idx).copy() | ||
# Remove previously performed background correction | ||
qpi.set_bg_data(None) | ||
return qpi | ||
|
||
def get_time(self, idx=0): | ||
"""Return the time of the QPImage data""" | ||
qpi = self._qpseries.get_qpimage(index=idx) | ||
if "time" in qpi.meta: | ||
return qpi.meta["time"] | ||
else: | ||
return 0 | ||
|
||
@staticmethod | ||
def verify(path): | ||
"""Verify that `path` has the qpimage file format | ||
Returns `True` if the file format matches. | ||
""" | ||
valid = False | ||
try: | ||
h5 = h5py.File(path, mode="r") | ||
qpi0 = h5["qpi_0"] | ||
except: | ||
pass | ||
else: | ||
if ("qpimage version" in qpi0.attrs and | ||
"phase" in qpi0 and | ||
"amplitude" in qpi0 and | ||
"bg_data" in qpi0["phase"] and | ||
"bg_data" in qpi0["amplitude"]): | ||
valid = True | ||
return valid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
from os.path import abspath, dirname, join | ||
import sys | ||
import tempfile | ||
|
||
import qpimage | ||
|
||
# Add parent directory to beginning of path variable | ||
sys.path.insert(0, dirname(dirname(abspath(__file__)))) | ||
import qpformat # noqa: E402 | ||
|
||
|
||
def test_load_data(): | ||
path = join(dirname(abspath(__file__)), "data/single_qpimage.h5") | ||
tf = tempfile.mktemp(suffix=".h5", prefix="qpformat_test_") | ||
qpi = qpimage.QPImage(h5file=path, h5mode="r") | ||
# generate qpseries hdf5 file | ||
with qpimage.QPSeries(qpimage_list=[qpi, qpi], | ||
h5file=tf, | ||
h5mode="a"): | ||
pass | ||
|
||
ds = qpformat.load_data(tf) | ||
assert len(ds) == 2 | ||
assert ds.path == tf | ||
assert ds.get_time(1) == 0 | ||
assert "SeriesHdf5Qpimage" in ds.__repr__() | ||
assert ds.get_qpimage(1) == qpi | ||
# cleanup | ||
try: | ||
os.remove(tf) | ||
except: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
# Run all tests | ||
loc = locals() | ||
for key in list(loc.keys()): | ||
if key.startswith("test_") and hasattr(loc[key], "__call__"): | ||
loc[key]() |