Skip to content

Commit 0188853

Browse files
authored
Merge pull request #318 from ercius/fix_remove_flyback
Fix remove_flyback algorithm
2 parents 08593dc + 7339501 commit 0188853

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

python/stempy/io/sparse_array.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,30 @@ def from_hdf5(cls, filepath, keep_flyback=True, **init_kwargs):
192192
scan_positions_group = f['electron_events/scan_positions']
193193
scan_shape = [scan_positions_group.attrs[x] for x in ['Nx', 'Ny']]
194194
frame_shape = [frames.attrs[x] for x in ['Nx', 'Ny']]
195-
195+
196196
if keep_flyback:
197197
data = frames[()] # load the full data set
198198
scan_positions = scan_positions_group[()]
199199
else:
200-
# Generate the original scan indices from the scan_shape
201-
orig_indices = np.ravel_multi_index([ii.ravel() for ii in np.indices(scan_shape)],scan_shape)
202-
# Remove the indices of the last column
203-
crop_indices = np.delete(orig_indices, orig_indices[scan_shape[0]-1::scan_shape[0]])
204-
# Load only the data needed
205-
data = frames[crop_indices]
206-
# Reduce the column shape by 1
207-
scan_shape[0] = scan_shape[0] - 1
200+
num = frames.shape[0] // np.prod(scan_shape, dtype=int) # number of frames per probe position
201+
data = np.empty(((scan_shape[0]-1) * scan_shape[1] * num), dtype=object)
202+
new_num_cols = scan_shape[0]-1 # number of columns without flyback
203+
for ii in range(scan_shape[1]):
204+
start = ii*new_num_cols*num # start of cropped data
205+
end = (ii+1)*new_num_cols*num
206+
start2 = ii*new_num_cols*num + num*ii # start of uncropped data
207+
end2 = (ii+1)*new_num_cols*num + num*ii
208+
data[start:end] = frames[start2:end2]
209+
scan_shape = (scan_shape[0]-1, scan_shape[1]) # update scan shape
208210
# Create the proper scan_positions without the flyback column
209-
scan_positions = np.ravel_multi_index([ii.ravel() for ii in np.indices(scan_shape)],scan_shape)
211+
scan_positions = np.ravel_multi_index([ii.ravel() for ii in np.indices(scan_shape)], scan_shape)
210212

211213
# Load any metadata
212214
metadata = {}
213215
if 'metadata' in f:
214216
load_h5_to_dict(f['metadata'], metadata)
215217

218+
# reverse the scan shape to match expected shape
216219
scan_shape = scan_shape[::-1]
217220

218221
if version >= 3:

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ def cropped_multi_frames_v2(cropped_multi_frames_data_v2):
109109
def cropped_multi_frames_v3(cropped_multi_frames_data_v3):
110110
return SparseArray.from_hdf5(cropped_multi_frames_data_v3, dtype=np.uint16)
111111

112+
@pytest.fixture
113+
def cropped_multi_frames_v3_noflyback(cropped_multi_frames_data_v3):
114+
return SparseArray.from_hdf5(cropped_multi_frames_data_v3,
115+
dtype=np.uint16, keep_flyback=False)
116+
112117
@pytest.fixture
113118
def simulate_sparse_array():
114119

tests/test_sparse_array.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,13 @@ def compare_with_sparse(full, sparse):
710710
assert np.array_equal(m_array[[False, True], 0][0], position_one)
711711

712712

713-
def test_keep_flyback(electron_data_small):
714-
flyback = SparseArray.from_hdf5(electron_data_small, keep_flyback=True)
715-
assert flyback.scan_shape[1] == 50
716-
no_flyback = SparseArray.from_hdf5(electron_data_small, keep_flyback=False)
717-
assert no_flyback.scan_shape[1] == 49
713+
def test_keep_flyback(cropped_multi_frames_v3, cropped_multi_frames_v3_noflyback):
714+
# Test keeping the flyback
715+
assert cropped_multi_frames_v3.scan_shape[1] == 20
716+
assert cropped_multi_frames_v3.num_frames_per_scan == 2
717+
# Test removing the flyback
718+
assert cropped_multi_frames_v3_noflyback.scan_shape[1] == 19
719+
assert cropped_multi_frames_v3_noflyback.num_frames_per_scan == 2
718720

719721
# Test binning until this number
720722
TEST_BINNING_UNTIL = 33

0 commit comments

Comments
 (0)