Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove flyback #304

Merged
merged 13 commits into from
Mar 27, 2024
7 changes: 4 additions & 3 deletions python/stempy/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,17 @@ def save_electron_counts(path, array):
"""
array.write_to_hdf5(path)

def load_electron_counts(path):
def load_electron_counts(path, keep_flyback=True):
"""Load electron counted data from an HDF5 file.

:param path: path to the HDF5 file.
:type path: str

:param keep_flyback: option to crop the flyback column during loading
:type keep_flyback: bool
:return: a SparseArray containing the electron counted data
:rtype: SparseArray
"""
return SparseArray.from_hdf5(path)
return SparseArray.from_hdf5(path, keep_flyback=keep_flyback)

def save_stem_images(outputFile, images, names):
"""Save STEM images to an HDF5 file.
Expand Down
26 changes: 20 additions & 6 deletions python/stempy/io/sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ def _validate(self):
raise Exception(msg)

@classmethod
def from_hdf5(cls, filepath, **init_kwargs):
def from_hdf5(cls, filepath, keep_flyback=True, **init_kwargs):
"""Create a SparseArray from a stempy HDF5 file

:param filepath: the path to the HDF5 file
:type filepath: str
:param keep_flyback: option to crop the flyback column during loading
:type keep_flyback: bool
:param init_kwargs: any kwargs to forward to SparseArray.__init__()
:type init_kwargs: dict

Expand All @@ -188,19 +190,31 @@ def from_hdf5(cls, filepath, **init_kwargs):

frames = f['electron_events/frames']
scan_positions_group = f['electron_events/scan_positions']

data = frames[()]
scan_shape = [scan_positions_group.attrs[x] for x in ['Nx', 'Ny']]
frame_shape = [frames.attrs[x] for x in ['Nx', 'Ny']]

scan_positions = scan_positions_group[()]

if keep_flyback:
data = frames[()] # load the full data set
scan_positions = scan_positions_group[()]
else:
# Generate the original scan indices from the scan_shape
orig_indices = np.ravel_multi_index([ii.ravel() for ii in np.indices(scan_shape)],scan_shape)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to use ii for indices? instead of i for ex.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never use single letter variables.

I once spent a full day debugging something where i was already used for sqrt(-1). Ever since, I always use ii, jj, kk, etc. for loops.

Also, when using the pdb debugger, c, q, n, etc. are reserved for interacting with the debugger (continue, quit, next, etc.). That messed me up a few times as well.

# Remove the indices of the last column
crop_indices = np.delete(orig_indices, orig_indices[scan_shape[0]-1::scan_shape[0]])
# Load only the data needed
data = frames[crop_indices]
# Reduce the column shape by 1
scan_shape[0] = scan_shape[0] - 1
# Create the proper scan_positions without the flyback column
scan_positions = np.ravel_multi_index([ii.ravel() for ii in np.indices(scan_shape)],scan_shape)

# Load any metadata
metadata = {}
if 'metadata' in f:
load_h5_to_dict(f['metadata'], metadata)

scan_shape = scan_shape[::-1]

if version >= 3:
# Convert to int to avoid integer division that results in
# a float
Expand Down
6 changes: 6 additions & 0 deletions tests/test_sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,12 @@ def compare_with_sparse(full, sparse):
assert np.array_equal(m_array[[False, True], 0][0], position_one)


def test_keep_flyback(electron_data_small):
flyback = SparseArray.from_hdf5(electron_data_small, keep_flyback=True)
assert flyback.scan_shape[1] == 50
no_flyback = SparseArray.from_hdf5(electron_data_small, keep_flyback=False)
assert no_flyback.scan_shape[1] == 49

# Test binning until this number
TEST_BINNING_UNTIL = 33

Expand Down
Loading