Skip to content

Commit

Permalink
Cache the current frame for fast access
Browse files Browse the repository at this point in the history
When working with large data on a slow filesystem, simply reading the
current displayed image gets to be really slow. This happens when
checking the intensity of different coordinates for instance (via
mouseover events). To improve performance, we can store the currently
displayed frame and simply access it if the user has not selected a
different frame.

We accomplish this here by storing the frame number (time) and the
current frame. These are both initialized to be `None` so that they will
fail any checks to see if the frame can be reused on the first call.
Within `get_image`, checks are added to see if the requested (or
default) frame number (time) are the same. If not, the new frame is
loaded and the time updated. By only updating the time after, this
ensures that some raised exception doesn't create an incorrectly
determined cached image. In either case the frame just now or already
loaded, is returned.
  • Loading branch information
jakirkham committed Dec 6, 2017
1 parent 64c6ab1 commit ab80716
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions mplview/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def set_images(self,
)

self.neuron_images = new_neuron_images
self._cur_img = None
self._cur_img_time = None
self.cmap = cmap
self.vmin = vmin
self.vmax = vmax
Expand Down Expand Up @@ -123,16 +125,18 @@ def get_image(self, i=None):
else:
i = 0

cur_img = self.neuron_images
if (len(self.neuron_images.shape) == 3):
cur_img = cur_img[i]
else:
cur_img = cur_img[...]
if i != self._cur_img_time:
if (len(self.neuron_images.shape) == 3):
self._cur_img = self.neuron_images[i]
self._cur_img_time = i
else:
self._cur_img = self.neuron_images[...]
self._cur_img_time = 0

cur_img = numpy.asarray(cur_img[...])
cur_img = cur_img.astype(float)
self._cur_img = numpy.asarray(self._cur_img[...])
self._cur_img = self._cur_img.astype(float)

return(cur_img)
return(self._cur_img)


def color_range_update(self, vmin, vmax):
Expand Down

0 comments on commit ab80716

Please sign in to comment.