Skip to content

Commit 3bb4539

Browse files
committed
Perform fancy indexing on csr_matrices
Rather than expanding the full csr_matrix into a numpy array, then performing fancy indexing on that, do the reverse: perform the fancy indexing on the csr_matrix, and then expand it into a numpy array. This has the potential to save huge amounts of computing time, since we do not have to create the entire dense array before performing the indexing. For the multiruby test case, this did not show any change to the performance. However, for a monolithic Eiger example from Josh Ward, it showed a 5x speed improvement. Signed-off-by: Patrick Avery <[email protected]>
1 parent 70ce7f4 commit 3bb4539

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

hexrd/imageseries/load/framecache.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,14 @@ def _load_framelist_if_needed(self):
209209

210210
def __getitem__(self, key):
211211
self._load_framelist_if_needed()
212-
return self._framelist[key].toarray()
212+
if isinstance(key, tuple):
213+
# Extract only what we need from the sparse array
214+
# using fancy indexing before we convert it to a
215+
# numpy array.
216+
sparse_frame = self._framelist[key[0]][*key[1:]]
217+
else:
218+
sparse_frame = self._framelist[key]
219+
return sparse_frame.toarray()
213220

214221
def __iter__(self):
215222
return ImageSeriesIterator(self)

hexrd/imageseries/load/hdf5.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def __del__(self):
6464
warnings.warn("HDF5ImageSeries could not close h5 file")
6565

6666
def __getitem__(self, key):
67+
if isinstance(key, tuple):
68+
# FIXME: we do not yet support fancy indexing here.
69+
# Fully expand the array then apply the fancy indexing.
70+
return self[key[0]][*key[1:]]
71+
6772
if self._ndim == 2:
6873
if key != 0:
6974
raise IndexError(

hexrd/instrument/hedm_instrument.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ def pull_spots(self, plane_data, grain_params,
17231723
contains_signal = False
17241724
for i_frame in frame_indices:
17251725
contains_signal = contains_signal or np.any(
1726-
ome_imgser[i_frame][ii, jj] > threshold
1726+
ome_imgser[i_frame, ii, jj] > threshold
17271727
)
17281728
compl.append(contains_signal)
17291729
patch_output.append((ii, jj, frame_indices))
@@ -1792,7 +1792,7 @@ def pull_spots(self, plane_data, grain_params,
17921792
contains_signal = False
17931793
patch_data_raw = []
17941794
for i_frame in frame_indices:
1795-
tmp = ome_imgser[i_frame][ijs[0], ijs[1]]
1795+
tmp = ome_imgser[i_frame, ijs[0], ijs[1]]
17961796
contains_signal = contains_signal or np.any(
17971797
tmp > threshold
17981798
)

0 commit comments

Comments
 (0)