Skip to content

Commit db91844

Browse files
committed
Tests for HDF5 load_manual
1 parent e4612a1 commit db91844

5 files changed

Lines changed: 70 additions & 5 deletions

File tree

phaser/hooks/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class LoadManualProps(Dataclass, kw_only=True):
5959
"""Detector ADU, representing the single-particle signal. Used to scale patterns."""
6060

6161
det_flips: t.Optional[t.Tuple[bool, bool, bool]] = None
62+
fftshifted: bool = False
63+
"""Whether patterns are fftshifted (zero-frequency in corner of array)"""
6264

6365
class RawDataHook(Hook[None, RawData]):
6466
known = {

phaser/hooks/io/manual.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def load_manual(args: None, props: LoadManualProps) -> RawData:
4040
logger.info("Loading as TIFF...")
4141
import tifffile
4242
patterns = numpy.asarray(tifffile.imread(path))
43-
elif ext in ('.h5', '.hdf5'):
43+
elif ext in ('.h5', '.hdf5', '.emd'):
4444
logger.info("Loading as HDF5...")
4545
patterns = _load_hdf5(path, props.key, logger)
4646
elif ext in ('.mat',):
@@ -100,9 +100,13 @@ def load_manual(args: None, props: LoadManualProps) -> RawData:
100100

101101
mask = numpy.ones_like(patterns, shape=patterns.shape[-2:])
102102

103+
if not props.fftshifted:
104+
patterns = numpy.fft.ifftshift(patterns, axes=(-1, -2))
105+
mask = numpy.fft.ifftshift(mask, axes=(-1, -2))
106+
103107
return {
104-
'patterns': numpy.fft.ifftshift(patterns, axes=(-1, -2)),
105-
'mask': numpy.fft.ifftshift(mask, axes=(-1, -2)),
108+
'patterns': patterns,
109+
'mask': mask,
106110
'sampling': sampling,
107111
'wavelength': wavelength,
108112
'probe_hook': None,
@@ -118,8 +122,8 @@ def _normalize_key(key: str) -> t.Tuple[str, ...]:
118122

119123

120124
_HDF5_KNOWN_KEYS: t.List[t.Tuple[str, ...]] = [
121-
('dp,',),
122-
('data,',),
125+
('dp',),
126+
('data',),
123127
]
124128

125129

tests/input_files/dp.h5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:e3c878e2bcf0e6875225880bf133ac812714abfbf0fcb7795561c7989355d330
3+
size 264192

tests/input_files/dp_customkey.h5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:ca1d16aad07e72c16826ebed48c527ef99c4bdfa95c02642b29ea5a491a69ab3
3+
size 266640

tests/test_load.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,59 @@ def test_load_manual_empad_v1(caplog, expected_raw_data: RawData):
7171
assert_raw_data_matches(raw_data, expected_raw_data, (4,))
7272

7373

74+
def test_load_manual_h5py(caplog, expected_raw_data: RawData):
75+
caplog.set_level(logging.INFO)
76+
get_backend_module("numpy")
77+
78+
hook = pane.from_data(
79+
{
80+
"type": "manual",
81+
"wavelength": 0.0251,
82+
"path": INPUT_FILES_PATH / "dp.h5",
83+
"diff_step": 0.4,
84+
"fftshifted": True,
85+
}, # type: ignore
86+
RawDataHook,
87+
)
88+
raw_data = hook(None)
89+
90+
assert caplog.record_tuples == [
91+
("phaser.hooks.io.manual", logging.INFO, "Loading as HDF5..."),
92+
("phaser.hooks.io.manual", logging.INFO, "Found patterns at key 'dp' (inferred) in HDF5 file."),
93+
("phaser.hooks.io.manual", logging.INFO, "Applying detector flips: [0, 0, 0] [y, x, transpose]"),
94+
("phaser.hooks.io.manual", logging.WARNING, "ADU not supplied for experimental dataset. This is not recommended."),
95+
]
96+
97+
assert_raw_data_matches(raw_data, expected_raw_data, (2, 2))
98+
99+
100+
def test_load_manual_h5py_customkey(caplog, expected_raw_data: RawData):
101+
caplog.set_level(logging.INFO)
102+
get_backend_module("numpy")
103+
104+
hook = pane.from_data(
105+
{
106+
"type": "manual",
107+
"wavelength": 0.0251,
108+
"path": INPUT_FILES_PATH / "dp_customkey.h5",
109+
"key": "a/b/c/d",
110+
"diff_step": 0.4,
111+
"fftshifted": True,
112+
}, # type: ignore
113+
RawDataHook,
114+
)
115+
raw_data = hook(None)
116+
117+
assert caplog.record_tuples == [
118+
("phaser.hooks.io.manual", logging.INFO, "Loading as HDF5..."),
119+
("phaser.hooks.io.manual", logging.INFO, "Loaded patterns from key 'a/b/c/d' in HDF5 file."),
120+
("phaser.hooks.io.manual", logging.INFO, "Applying detector flips: [0, 0, 0] [y, x, transpose]"),
121+
("phaser.hooks.io.manual", logging.WARNING, "ADU not supplied for experimental dataset. This is not recommended."),
122+
]
123+
124+
assert_raw_data_matches(raw_data, expected_raw_data, (2, 2))
125+
126+
74127
def test_load_manual_tiff(caplog, expected_raw_data: RawData):
75128
caplog.set_level(logging.INFO)
76129
get_backend_module("numpy")

0 commit comments

Comments
 (0)