Skip to content

Commit

Permalink
feat: extract meta data from QPSeries/QPImage data files
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Sep 27, 2018
1 parent 80f34e7 commit 5982bae
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.4.0
- feat: extract meta data from QPSeries/QPImage data files
0.3.5
- fix: single_tif_phaiscs (SID4Bio) contains two phase images, the second
of which is recorded at a different time point than the intensity
Expand Down
7 changes: 7 additions & 0 deletions qpformat/file_formats/series_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class SeriesHdf5Qpimage(SeriesData):
def __init__(self, *args, **kwargs):
super(SeriesHdf5Qpimage, self).__init__(*args, **kwargs)
self._dataset = None
# update meta data
with h5py.File(self.path, mode="r") as h5:
attrs = dict(h5["qpi_0"].attrs)
for key in qpimage.meta.DATA_KEYS:
if (key not in self.meta_data
and key in attrs):
self.meta_data[key] = attrs[key]

def __len__(self):
with self._qpseries() as qps:
Expand Down
10 changes: 10 additions & 0 deletions qpformat/file_formats/single_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class SingleHdf5Qpimage(SingleData):
"""
storage_type = "phase,amplitude"

def __init__(self, *args, **kwargs):
super(SingleHdf5Qpimage, self).__init__(*args, **kwargs)
# update meta data
with h5py.File(self.path, mode="r") as h5:
attrs = dict(h5.attrs)
for key in qpimage.meta.DATA_KEYS:
if (key not in self.meta_data
and key in attrs):
self.meta_data[key] = attrs[key]

def get_qpimage(self, idx=0):
"""Return background-corrected QPImage"""
if self._bgdata:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_series_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ def test_load_data():
pass


def test_meta_extraction():
path = datapath / "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",
meta_data={"wavelength": 111e-9,
"pixel size": .12}):
pass

ds = qpformat.load_data(tf)

assert ds.meta_data["wavelength"] == 111e-9
assert ds.meta_data["pixel size"] == .12

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_meta_override():
path = datapath / "single_qpimage.h5"
tf = tempfile.mktemp(suffix=".h5", prefix="qpformat_test_")
Expand Down
22 changes: 22 additions & 0 deletions tests/test_single_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ def test_load_data():
assert np.allclose(qpd.pha, qpi.pha)


def test_meta_extraction():
path = datapath / "single_qpimage.h5"
tf = tempfile.mktemp(suffix=".h5", prefix="qpformat_test_")
qpi = qpimage.QPImage(h5file=path, h5mode="r").copy()
qpi["wavelength"] = 345e-9
qpi["pixel size"] = .1e-6
qpi["medium index"] = 1.336
qpi.copy(tf)

ds = qpformat.load_data(tf)

assert ds.meta_data["wavelength"] == 345e-9
assert ds.meta_data["pixel size"] == .1e-6
assert ds.meta_data["medium index"] == 1.336

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_meta_override():
path = datapath / "single_qpimage.h5"
tf = tempfile.mktemp(suffix=".h5", prefix="qpformat_test_")
Expand Down

0 comments on commit 5982bae

Please sign in to comment.